mysqli bind_param() 应该是一个引用,给定值 [英] mysqli bind_param() expected to be a reference, value given

查看:39
本文介绍了mysqli bind_param() 应该是一个引用,给定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法弄清楚,是什么导致错误 mysqli_stmt::bind_param() 的参数 3 预期为引用,值在...中给出

Can't figure out, whats causing error Parameter 3 to mysqli_stmt::bind_param() expected to be a reference, value given in...

PDO
$query = "INSERT INTO test (id,row1,row2,row3) VALUES (?,?,?,?)";
$params = array(1,"2","3","4");
$param_type = "isss";
$sql_stmt = mysqli_prepare ($mysqli, $query);
call_user_func_array('mysqli_stmt_bind_param', array_merge(array($sql_stmt, $param_type), $params));
mysqli_stmt_execute($sql_stmt);

也试过OOP

OOP
$insert_stmt = $mysqli->prepare($query);
array_unshift($params, $param_type);
call_user_func_array(array($insert_stmt, 'bind_param'), $params);
$insert_stmt->execute();

但同样的错误,只是现在参数 2 导致问题.

But same error, only that now Parameter 2 is causing problem.

那么,$params 有什么问题?我需要 $params 是一个值数组.

So, what's wrong with $params? I need $params to be an array of values.

推荐答案

UPDATE

这个答案已经过时了.请在较新的 PHP 版本中使用扩展运算符,例如 Stacky 的回答.

This answer is outdated. Please use the spread operator in newer PHP versions like answered by Stacky.

来自 php 文档:

将 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.

在页面上mysqli-stmt.bind-param 您有不同的解决方案:

And on the page mysqli-stmt.bind-param you have different solutions:

例如:

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

这篇关于mysqli bind_param() 应该是一个引用,给定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆