Advertisement
AndrewLarsen

MySQLi class

Feb 3rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.93 KB | None | 0 0
  1. <?php
  2.  
  3. /* Config */
  4. const DB_HOST = 'localhost';
  5. const DB_USER = 'root';
  6. const DB_PASS = '';
  7. const DB_NAME = 'testDb';
  8. const DB_CHARSET = 'utf8';
  9.  
  10. const DEBUG = 1;
  11.  
  12. /* New DB instance */
  13. $db = new DB();
  14.  
  15. /* Test variables */
  16. $id = 1;
  17. $something = "456";
  18.  
  19. /* Prepared statement */
  20. $query = 'SELECT * FROM `test1` WHERE `id` = ? AND `something` = ?';
  21. $sql = $db->query(
  22.     $query,
  23.     array($id, $something)
  24. );
  25.  
  26. /* Normal query */
  27. $query = 'SELECT * FROM `test2` WHERE `id` = 1';
  28. $sql = $db->query($query);
  29.  
  30. /* Fetch data */
  31. $data = $db->fetch_all();
  32.  
  33. /* Display result */
  34. print_r($data);
  35.  
  36. /* Close db */
  37. $db->close();
  38.  
  39.  
  40. /**
  41.  * MySQLi db class by Andrew Larsen
  42.  */
  43. class DB
  44. {
  45.     private $mysqli;
  46.     private $stmt;
  47.     private $lastPrepared;
  48.  
  49.     public function __construct($host = DB_HOST, $user = DB_USER, $pass = DB_PASS, $dbname = DB_NAME, $charset = DB_CHARSET){
  50.         $this->mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  51.  
  52.         if (!$this->mysqli) {
  53.             if (DEBUG)
  54.                 echo 'Code: ' . mysqli_connect_errno() . ' - Message: ' . mysqli_connect_error();
  55.        
  56.             return false;
  57.         }
  58.    
  59.         $this->mysqli->set_charset(DB_CHARSET);
  60.     }
  61.  
  62.     private function bind($parameters) {
  63.         $param_type = '';
  64.         $param_values = array();
  65.  
  66.         foreach ($parameters as $key => $value) {
  67.             $param_values[] = $value;
  68.  
  69.             if (is_string($value))
  70.                 $param_type .= 's'; // string
  71.             else if (is_int($value))
  72.                 $param_type .= 'i'; // int
  73.             else if (is_float($value))
  74.                 $param_type .= 'd'; // double
  75.             else
  76.                 $param_type .= 'b'; // blob and unknown
  77.         }
  78.  
  79.         $params = array();
  80.  
  81.         /* with call_user_func_array, array params must be passed by reference */
  82.         $params[] = &$param_type;
  83.  
  84.         for ($i = 0; $i < count($param_values); $i++) {
  85.             /* with call_user_func_array, array params must be passed by reference */
  86.             $params[] = &$param_values[$i];
  87.         }
  88.  
  89.         call_user_func_array(array($this->stmt, 'bind_param'), $params);
  90.     }
  91.  
  92.     private function prepare($query) {
  93.         if (!$this->stmt = $this->mysqli->prepare($query)) {
  94.             if (DEBUG)
  95.                 echo "Prepare failed: (" . $this->mysqli->errno . ") " . $this->mysqli->error;
  96.            
  97.             return false;
  98.         }
  99.  
  100.         return true;
  101.     }
  102.  
  103.     private function execute() {
  104.         if (!$this->stmt->execute()) {
  105.             if (DEBUG)
  106.                 echo "Execute failed: (" . $this->stmt->errno . ") " . $this->stmt->error;
  107.            
  108.             return false;
  109.         }
  110.  
  111.         return true;
  112.     }
  113.  
  114.     public function query($query, $parameters = array()) {
  115.         if (is_array($parameters) && count($parameters) > 0) {
  116.             $this->lastPrepared = true;
  117.            
  118.             if ($this->prepare($query) === false)
  119.                 return false;
  120.            
  121.             $this->bind($parameters);
  122.             if ($this->execute() === false)
  123.                 return false;
  124.         } else {
  125.             $this->lastPrepared = false;
  126.            
  127.             if (!$this->stmt = $this->mysqli->query($query)) {
  128.                 if (DEBUG)
  129.                     echo "Query failed: (" . $this->mysqli->errno . ") " . $this->mysqli->error;
  130.                
  131.                 return false;
  132.             }
  133.         }
  134.  
  135.         return $this->stmt;
  136.     }
  137.  
  138.     public function fetch_all($autoClean = true) {
  139.         if ($this->lastPrepared) {
  140.             $result = $this->stmt->get_result();
  141.             $data = $result->fetch_all(MYSQLI_ASSOC);
  142.         } else {
  143.             $data = $this->stmt->fetch_all(MYSQLI_ASSOC);
  144.         }
  145.  
  146.         if ($autoClean) {
  147.             $this->stmt->free_result();
  148.  
  149.             if ($this->lastPrepared) $this->stmt->close();
  150.         }
  151.  
  152.         if (count($data) == 1)
  153.             return $data[0];
  154.  
  155.         return $data;
  156.     }
  157.  
  158.     public function begin($option = 'MYSQLI_TRANS_START_READ_ONLY') {
  159.         $options = array(
  160.             'MYSQLI_TRANS_START_READ_ONLY',
  161.             'MYSQLI_TRANS_START_READ_WRITE',
  162.             'MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT'
  163.         );
  164.  
  165.         if (!in_array($option, $options))
  166.             return false;
  167.  
  168.         $this->mysqli->begin_transaction($option);
  169.     }
  170.  
  171.     public function commit() {
  172.         $this->mysqli->commit();
  173.     }
  174.  
  175.     public function autocommit($bool) {
  176.         $this->mysqli->autocommit($bool);
  177.     }
  178.  
  179.     public function close() {
  180.         $this->mysqli->close();
  181.     }
  182.    
  183.     public function stmtClose()
  184.     {
  185.         $this->stmt->close();
  186.     }
  187.  
  188. }
  189.  
  190. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement