php - mysqli bind_param() 期望是一个引用,给定值

标签 php arrays mysqli

无法弄清楚,是什么导致了错误参数 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 导致了问题。

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

最佳答案

更新

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

来自 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.

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

例如:

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

关于php - mysqli bind_param() 期望是一个引用,给定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16120822/

相关文章:

php - Shopify - 如何在 index.liquid 上显示 "Browse by subcategories"表单?

PHP in_array 函数,为什么这不起作用?

php - 如何在 mysqli 中转换 mysql_result?

PHP $_GET 方法不起作用/遵循系统

php - 在 CI 中使用加载数据 InFile 上传 500000 条记录,但有时并非所有记录都插入

php - "mysqli select"准备语句中的 php 函数出错

PHP header 位置重定向到当前文件夹的根目录

c# - 如何从包含多个对象的 JSON 文件中反序列化

php - 什么是更快的平面文件或 MySQL RAM 数据库?

c - 尝试查找 int 数组的元素是否等于在 C 中输入的数字