改寫 mysqli_stmt::bind_param

來源:http://php.net/manual/en/mysqli-stmt.bind-param.php#110363

讓 bind_param 時可以跑迴圈,順便參考一下別人的改法。

<?php 

class db extends mysqli { 
    public function prepare($query) { 
        return new stmt($this,$query); 
    } 
} 

class stmt extends mysqli_stmt { 
    public function __construct($link, $query) { 
        $this->mbind_reset(); 
        parent::__construct($link, $query); 
    } 

    public function mbind_reset() { 
        unset($this->mbind_params); 
        unset($this->mbind_types); 
        $this->mbind_params = array(); 
        $this->mbind_types = array(); 
    } 
    
    //use this one to bind params by reference 
    public function mbind_param($type, &$param) { 
        $this->mbind_types[0].= $type; 
        $this->mbind_params[] = &$param; 
    } 
    
    //use this one to bin value directly, can be mixed with mbind_param() 
    public function mbind_value($type, $param) { 
        $this->mbind_types[0].= $type; 
        $this->mbind_params[] = $param; 
    } 
    
    
    public function mbind_param_do() { 
        $params = array_merge($this->mbind_types, $this->mbind_params); 
        return call_user_func_array(array($this, 'bind_param'), $this->makeValuesReferenced($params)); 
    } 
    
    private function makeValuesReferenced($arr){ 
        $refs = array(); 
        foreach($arr as $key => $value) 
        $refs[$key] = &$arr[$key]; 
        return $refs; 

    } 
    
    public function execute() { 
        if(count($this->mbind_params)) 
            $this->mbind_param_do(); 
            
        return parent::execute(); 
    } 
    
    private $mbind_types = array(); 
    private $mbind_params = array(); 
} 

$search1 = "test1"; 
$search2 = "test2"; 

$_db = new db("host","user","pass","database"); 
$query = "SELECT name FROM table WHERE col1=? AND col2=?"; 
$stmt = $_db->prepare($query); 

$stmt->mbind_param('s',$search1); 
//this second call is the cool thing!!! 
$stmt->mbind_param('s',$search2); 

$stmt->execute(); 

//this would still work! 
//$search1 = "test1changed"; 
//$search2 = "test2changed"; 
//$stmt->execute(); 

... 

$stmt->store_result(); 
$stmt->bind_result(...); 
$stmt->fetch(); 
?>