trollolodn

#php #mysqli Prepare with params

Feb 4th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.60 KB | None | 0 0
  1. <?php
  2.  
  3. class db extends mysqli {
  4.     public function prepare($query) {
  5.         return new stmt($this,$query);
  6.     }
  7. }
  8.  
  9. class stmt extends mysqli_stmt {
  10.     public function __construct($link, $query) {
  11.         $this->mbind_reset();
  12.         parent::__construct($link, $query);
  13.     }
  14.  
  15.     public function mbind_reset() {
  16.         unset($this->mbind_params);
  17.         unset($this->mbind_types);
  18.         $this->mbind_params = array();
  19.         $this->mbind_types = array();
  20.     }
  21.    
  22.     //use this one to bind params by reference
  23.     public function mbind_param($type, &$param) {
  24.         $this->mbind_types[0].= $type;
  25.         $this->mbind_params[] = &$param;
  26.     }
  27.    
  28.     //use this one to bin value directly, can be mixed with mbind_param()
  29.     public function mbind_value($type, $param) {
  30.         $this->mbind_types[0].= $type;
  31.         $this->mbind_params[] = $param;
  32.     }
  33.    
  34.    
  35.     public function mbind_param_do() {
  36.         $params = array_merge($this->mbind_types, $this->mbind_params);
  37.         return call_user_func_array(array($this, 'bind_param'), $this->makeValuesReferenced($params));
  38.     }
  39.    
  40.     private function makeValuesReferenced($arr){
  41.         $refs = array();
  42.         foreach($arr as $key => $value)
  43.         $refs[$key] = &$arr[$key];
  44.         return $refs;
  45.  
  46.     }
  47.    
  48.     public function execute() {
  49.         if(count($this->mbind_params))
  50.             $this->mbind_param_do();
  51.            
  52.         return parent::execute();
  53.     }
  54.    
  55.     private $mbind_types = array();
  56.     private $mbind_params = array();
  57. }
  58.  
  59.  
  60.  
  61.  
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment