2016-07-26 62 views
1

这是插入行的简单代码。 它运行良好的PHP 5.6,但通过PHP 7.0.9我得到的错误:“参数3 mysqli_stmt_bind_param()预计是一个参考”。参数3到mysqli_stmt_bind_param()有望成为一个参考:php5.6 vs php7

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

它是什么错误?

+0

它需要一个参考,所以你不能使用array_merge的返回值,你需要为它为参数引用3+实际变量。 – Devon

+1

从我的测试中,您有refValues函数应该照顾参考。它在7.0.5上适用于我。 – Devon

+0

@Devon,谢谢! – ivhome

回答

1

答案是改变refValues功能

function refValues(&$arr)