【发布时间】:2016-07-26 14:22:00
【问题描述】:
这是插入行的简单代码。 它在 php 5.6 中运行良好,但在 php 7.0.9 中我收到错误消息:“mysqli_stmt_bind_param() 的参数 3 应为参考”。
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;
}
...
$sql = "INSERT INTO table (player_id,ctime) VALUES(?,?)";
$types = "ii";
$args = array(10, time());
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name, $db_port, $db_sock);
if(!$conn)
throw new Exception("Could not connect to mysql server");
$stmt = mysqli_prepare($conn, $sql);
if(!$stmt)
throw new Exception("Could not prepare sql");
$res = call_user_func_array('mysqli_stmt_bind_param', array_merge(array($stmt, $types), refValues($args)));
if(!$res)
throw new Exception("Could not bind params");
if(!mysqli_stmt_execute($stmt))
throw new Exception("Could not execute stmt");
怎么了?
【问题讨论】:
-
需要引用,所以不能使用array_merge的返回值,需要提供实际变量供参数3+引用。
-
根据我的测试,您拥有的 refValues 函数应该负责引用。它适用于 7.0.5。