問題描述
我正在為 MySQLi 編寫一個(gè)包裝類.在那里,我正在編寫一個(gè)函數(shù)來接受查詢和可變數(shù)量的參數(shù),我可以在其中調(diào)用 mysqli_stmt::bind_param
.代碼如下:
I am writing a wrapper class for MySQLi. In there, I am writing a function to accept a query and variable number of parameters where I can invoke mysqli_stmt::bind_param
. Here is the code:
<?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
}
}
....
}
?>
這是我調(diào)用函數(shù)的方式:
Here's how I am calling the function:
<?php
$db = new DbHelper();
$params = array('i', $stateID);
$result = $db->Execute('SELECT * FROM mst_cities WHERE State_ID = ?', $params);
?>
當(dāng)我運(yùn)行代碼時(shí),我收到如下警告:警告:mysqli_stmt::bind_param() 的參數(shù) 2 應(yīng)為引用,值在......中給出.
When I run the code, I get an warning as this:
Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in......
我該怎么辦?
推薦答案
來自 文檔:
注意:
將 mysqli_stmt_bind_param() 與 call_user_func_array() 結(jié)合使用時(shí)必須小心.請注意,mysqli_stmt_bind_param() 需要通過引用傳遞參數(shù),而 call_user_func_array() 可以接受可以表示引用或值的變量列表作為參數(shù).
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.
評論中有一些解決方法,例如 看到這個(gè):
There are some workarounds in the comments, for example see this:
call_user_func_array(array($stmt, 'bind_param'), 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;
}
這篇關(guān)于不能在 mysqli_stmt 對象上使用 call_user_func_array的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!