php5.3 - mysqli_stmt:bind_params 带有 call_user_func_array 警告
可能的重复:
是否可以传递参数通过引用使用 call_user_func_array()?
我有以下代码行在 PHP 5.1 中有效,但在 PHP 5.3 中无效.
I have the following line of code which worked in PHP 5.1, but isn't working in PHP 5.3.
$input = array('ss','john','programmer');
call_user_func_array(array($mysqli_stmt, 'bind_param'), $input);
在 PHP 5.3 中,我收到以下警告消息:
In PHP 5.3, I get the following warning message:
警告:mysqli_stmt::bind_param() 的参数 2 应为参考,值在/var/www/startmission/em/class/cls.data_access_object.php 第 785 行
Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in /var/www/startmission/em/class/cls.data_access_object.php on line 785
我将代码更改为以下内容并且它起作用了:
I changed the code to the following and it worked:
$a = 'johnl';
$b = 'programmer';
$mysqli_stmt->bind_param('ss',$a,$b);
我在 php 文档中找到了这个:
I found this in the php documentation:
在使用 mysqli_stmt_bind_param() 时必须小心与 call_user_func_array() 结合使用.注意mysqli_stmt_bind_param() 需要通过引用传递参数,而 call_user_func_array() 可以接受一个列表作为参数可以表示引用或值的变量.
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 + bind_params 的功能,以便我可以在运行时动态绑定变量?
So my question is, how do I replicate the functionality of the call_user_func_array + bind_params such that i can dynamically bind variables at run time?
推荐答案
我在 fabio at Kidopi dot com dot br3 年前在 mysqli_stmt::bind_param() 的 PHP 手册页
(略有修改):
I found the answer to my problem in a user note by fabio at kidopi dot com dot br3 years ago on the PHP manual page of mysqli_stmt::bind_param()
(slightly modified):
在迁移到 php 5.3 后,我曾经在使用 call_user_func_array
和 bind_param
时遇到问题.
I used to have problems with
call_user_func_array
andbind_param
after migrating to php 5.3.
原因是 5.3 需要数组值作为引用,而 5.2 使用真实值(但也使用引用).所以我创建了一个辅助函数来帮助我解决这个问题:
The cause is that 5.3 requires array values as reference while 5.2 worked with real values (but also with references). So I created a secondary helper function to help me with this:
function refValues($arr)
{
$refs = array();
foreach ($arr as $key => $value)
{
$refs[$key] = &$arr[$key];
}
return $refs;
}
并将我以前的功能从:
call_user_func_array(array($this->stmt, "bind_param"), $this->values);
到:
call_user_func_array(array($this->stmt, "bind_param"), refValues($this->values));
这样我的 db 函数就可以在 PHP 5.2/5.3 服务器上继续工作.
This way my db functions keep working in PHP 5.2/5.3 servers.
相关文章