Advertisement
Guest User

Untitled

a guest
May 6th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.54 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', '1');
  4. /**
  5.  * DBBase.php
  6.  * Tietokantakäsittelyn perusluokka. Sisältää perusfunktiot (select, insert, update, delete)
  7.  *
  8.  * @author onik
  9.  *
  10.  */
  11.  
  12. class DBBase {
  13.    
  14.     // Määritellään perusmuuttujat tietokantayhteyttä varten
  15.        
  16.     public $server = "localhost";
  17.     public $database = "fivezone3";
  18.     public $username = "fivezone";
  19.     public $password = "JuT5ehUn";
  20.     public $sql;
  21.     public $result;
  22.    
  23.     /**
  24.      * Lataa tarvittavat luokat automaattisesti
  25.      *
  26.      * @param $classname    luokan nimi
  27.      * @return void
  28.      */
  29.    
  30.     function __autoload($classname) {
  31.         require_once $classname . '.php';
  32.     }
  33.        
  34.  
  35.     /**
  36.      * Suorittaa kyselyn tietokantaan (geneerinen query)
  37.      *
  38.      * @param $query    kysely SQL-kielellä
  39.      *
  40.      * @return kyselyn tulos / error
  41.      */
  42.    
  43.     function doQuery($q) {
  44.         $sql = new mysqli($this->server, $this->username, $this->password, $this->database);
  45.         $result = $sql->query($q);
  46.         if($result == false) {
  47.             return false;
  48.         }
  49.         $sql->close();
  50.         return $result;
  51.     }
  52.    
  53.     /**
  54.      * Select: Suorittaa select kyselyn, palauttaa tulokset
  55.      *
  56.      * @param $what SELECT ?
  57.      * @param $from FROM ?
  58.      * @param $where    WHERE ?
  59.      * @param $sort ORDER BY ?
  60.      * @param $limit    LIMIT ?
  61.      * @return kyselyn tulos / error
  62.      */
  63.    
  64.     function select($what, $from, $where = "", $sort = "", $limit = "") {
  65.  
  66.        
  67.         if($what == "" || $from == "") {
  68.             return false;
  69.         }
  70.        
  71.         $tmp = "SELECT " . $what . " FROM " . $from;
  72.         if($where != "") {
  73.             $tmp .= " WHERE " . $where;
  74.         }
  75.         if($sort != "") {
  76.             $tmp .= " ORDER BY " . $sort;
  77.         }
  78.         if($limit != "") {
  79.             $tmp .= " LIMIT " . $limit;
  80.         }
  81.         print_r($tmp);
  82.         if(!$result = $this->doQuery($tmp)) {
  83.             return false;
  84.         } else {
  85.             return $result;
  86.         }
  87.     }
  88.    
  89.     /**
  90.      * Suorittaa insert kyselyn, palauttaa tuloksen
  91.      *
  92.      * @param $table    INSERT INTO ?
  93.      * @param $fields   (?) array
  94.      * @param $values   VALUES (?) array
  95.      * @return kyselyn tulos / error
  96.      */
  97.    
  98.     function insert($table, $fields, $values) {
  99.        
  100.         if ($table == "") {
  101.             return false;
  102.         }
  103.        
  104.         if (count($fields) == 0) {
  105.             return false;
  106.         }
  107.        
  108.         if (count($values) == 0) {
  109.             return false;
  110.         }
  111.        
  112.         if (count($fields) != count($values)){
  113.             return false;
  114.             }
  115.        
  116.                
  117.         for ($j=0;$j<count($values);$j++) {
  118.             $values[$j] = "'" . $values[$j] . "'";
  119.         }
  120.        
  121.         $tmp = "INSERT INTO " . $table . " (";
  122.  
  123.         for ($i=0;$i<count($fields)-1;$i++) {
  124.             $tmp .= $fields[$i] . ", ";
  125.         }
  126.         $tmp .= $fields[count($fields)-1];
  127.        
  128.         $tmp .= ") VALUES " . "(";
  129.  
  130.         for ($i=0;$i<count($values)-1;$i++) {
  131.             $tmp .= $values[$i] . ", ";
  132.         }
  133.         $tmp .= $values[count($values)-1];
  134.        
  135.         $tmp .= ")";
  136.                        
  137.         if(!$result = $this->doQuery($tmp)) {
  138.             return "ERROR";
  139.         } else {
  140.             return true;
  141.         }
  142.     }
  143.    
  144.     /**
  145.      * Suorittaa update kyselyn, palauttaa tuloksen
  146.      *
  147.      * @param $table    UPDATE ?
  148.      * @param $fields   ?= array
  149.      * @param $values   ? array
  150.      * @param $where    WHERE ?
  151.      * @param $sort     ORDER BY ?
  152.      * @param $limit    LIMIT ?
  153.      * @return kyselyn tulos / error
  154.      */
  155.    
  156.     function update($table, $fields, $values, $where = "", $sort = "", $limit = "") {
  157.                
  158.         if ($table == "") {
  159.             return false;
  160.         }
  161.        
  162.         if (count($fields) == 0) {
  163.             return false;
  164.         }
  165.        
  166.         if (count($values) == 0) {
  167.             return false;
  168.         }
  169.        
  170.         if (count($fields) != count($values)){
  171.             return false;
  172.             }
  173.        
  174.         for ($i=0;$i<count($fields);$i++) {
  175.             $fields[$i] = "" . $fields[$i] . "";
  176.         }
  177.        
  178.         for ($j=0;$j<count($values);$j++) {
  179.             $values[$j] = "'" . $values[$j] . "'";
  180.         }
  181.        
  182.         $tmp = "UPDATE " . $table . " SET ";
  183.  
  184.         for ($i=0;$i<count($fields)-1;$i++) {
  185.             $tmp .= $fields[$i] . "=" . $values[$i] . ", ";
  186.         }
  187.         $tmp .= $fields[$i] . "=" . $values[$i];
  188.        
  189.         if($where != "") {
  190.             $tmp .= " WHERE " . $where;
  191.         }
  192.         if($sort != "") {
  193.             $tmp .= " ORDER BY " . $sort;
  194.         }
  195.         if($limit != "") {
  196.             $tmp .= " LIMIT " . $limit;
  197.         }
  198.         echo($tmp);            
  199.         if(!$result = $this->doQuery($tmp)) {
  200.             return "error";
  201.         } else {
  202.             return $result;
  203.         }
  204.        
  205.     }
  206.    
  207.     /**
  208.      * Poistaa kyselyn mukaiset tiedot
  209.      *
  210.      * @param $from DELETE FROM ?
  211.      * @param $where    WHERE ?
  212.      * @param $limit    LIMIT ?
  213.      * @return kyselyn tulos / error
  214.      */
  215.    
  216.     function delete($from, $where, $limit = "") {
  217.                
  218.         if($from == "") {
  219.             return false;
  220.         }
  221.        
  222.         $tmp = "DELETE FROM " . $from;
  223.         if($where != "") {
  224.             $tmp .= " WHERE " . $where;
  225.         }
  226.         if($limit != "") {
  227.             $tmp .= " LIMIT " . $limit;
  228.         }
  229.        
  230.         if(!$result = $this->doQuery($tmp)) {
  231.             return "ERROR";
  232.         } else {
  233.             return $result;
  234.         }
  235.        
  236.     }
  237. }
  238.  
  239. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement