我正在为
MySQLi编写一个包装类.在那里,我正在编写一个函数来接受一个查询和可变数量的参数,我可以在其中调用MysqLi_stmt :: bind_param.这是代码:
<?PHP class DbHelper { .... public function Execute($query,$params){ $this->open(); # Opens a connection to the database using MysqLi API $stmt = $this->MysqLi->prepare($query); try{ $result = call_user_func_array(array($stmt,'bind_param'),$params); } catch(Exception $ex){ # Handle Exception } } .... } ?>
<?PHP $db = new DbHelper(); $params = array('i',$stateID); $result = $db->Execute('SELECT * FROM mst_cities WHERE State_ID = ?',$params); ?>
当我运行代码时,我得到一个警告:
警告:参数2到MysqLi_stmt :: bind_param()应该是一个引用,值在……中给出
我该怎么办?
从
docs:
Note:
Care must be taken when using MysqLi_stmt_bind_param() in conjunction with call_user_func_array(). Note that MysqLi_stmt_bind_param() requires parameters to be passed by reference,whereas call_user_func_array() can accept as a parameter a list of variables that can represent references or values.
call_user_func_array(array($stmt,refValues($params)); function refValues($arr) { if (strnatcmp(PHPversion(),'5.3') >= 0) //Reference is required for PHP 5.3+ { $refs = array(); foreach($arr as $key => $value) $refs[$key] = &$arr[$key]; return $refs; } return $arr; }