php5.3 - mysqli_stmt:bind_params 带有 call_user_func_array 警告

php5.3 - mysqli_stmt:bind_params with call_user_func_array warnings(php5.3 - mysqli_stmt:bind_params 带有 call_user_func_array 警告)
本文介绍了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_arraybind_param 时遇到问题.

I used to have problems with call_user_func_array and bind_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.

这篇关于php5.3 - mysqli_stmt:bind_params 带有 call_user_func_array 警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)