【问题标题】:Parameter 3 to mysqli_stmt_bind_param() expected to be a reference : php5.6 vs php7mysqli_stmt_bind_param() 的参数 3 应作为参考:php5.6 vs php7
【发布时间】: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。

标签: php mysql php-7 php-5.6


【解决方案1】:

答案是将 refValues 函数改为

function refValues(&$arr)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2019-06-14
    • 1970-01-01
    相关资源
    最近更新 更多