这是插入行的简单代码。它在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");
怎么了?
答案是将refValues函数更改为
function refValues(&$arr)