mysqli - php5.3 - mysqli_stmt :bind_params with call_user_func_array warnings

标签 mysqli php

<分区>

Possible Duplicate:
Is it possible to pass parameters by reference using call_user_func_array()?

我有以下代码行在 PHP 5.1 中有效,但在 PHP 5.3 中无效。

$input = array('ss','john','programmer');
call_user_func_array(array($mysqli_stmt, 'bind_param'), $input);

在 PHP 5.3 中,我收到以下警告消息:

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

我将代码更改为以下并且它有效:

$a = 'johnl';
$b = 'programmer';
$mysqli_stmt->bind_param('ss',$a,$b);

我在 php 文档中找到了这个:

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 的功能,以便我可以在运行时动态绑定(bind)变量?

最佳答案

我在 fabio at kidopi dot com dot br3 years ago on the PHP manual page of mysqli_stmt::bind_param() 的用户注释中找到了问题的答案(稍作修改):

I used to have problems with call_user_func_array and bind_param after migrating to php 5.3.

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; 
}

and changed my previous function from:

call_user_func_array(array($this->stmt, "bind_param"), $this->values); 

to:

call_user_func_array(array($this->stmt, "bind_param"), refValues($this->values)); 

This way my db functions keep working in PHP 5.2/5.3 servers.

关于mysqli - php5.3 - mysqli_stmt :bind_params with call_user_func_array warnings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3681262/

相关文章:

php - 具有多个条件的搜索功能 - PHP/MySQL

PHP 和 MySQLi 将查询结果转换为字符串时出错

PHP - 来自 HTML 表单的未定义索引 - 显示为 NULL

php - 如何从给定的 MySQL 表中获取列名?

php - WHERE NOT EXISTS 语法错误

php - 安装 PHPUnit 的问题

PHP/MySQL : The right way creating a big website database based

php - 如果我们必须使用未知用户的 css 文件,应该注意什么?

PHP parse_ini_file 和 includes 不工作(Ubuntu 桌面 LAMP)

php - 如何获取 PHP 字符串的最后 7 个字符?