mysqli_stmt_bind_param()的参数3应该是一个参考:php5.6 vs php7

时间:2016-07-26 14:22:00

标签: php mysql php-7 php-5.6

这是插入行的简单代码。 它运行良好的PHP 5.6,但通过PHP 7.0.9我得到错误:“参数3到mysqli_stmt_bind_param()期望是一个参考”。

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");

有什么不对?

1 个答案:

答案 0 :(得分:1)

答案是将refValues函数更改为

function refValues(&$arr)