当PHP版本高于5.3以后,在向mysqli_stmt_bind_param函数传递参数时需要通过引用传递。因此,如下代码会报错:
$query = "INSERT INTO test (id,row1,row2,row3) VALUES (?,?,?,?)"; $params = array(1,"2","3","4"); $param_type = "isss"; $sql_stmt = mysqli_prepare ($mysqli, $query); call_user_func_array('mysqli_stmt_bind_param', array_merge(array($sql_stmt, $param_type), $params)); mysqli_stmt_execute($sql_stmt);
正确的做法应该将传递给 mysqli_stmt bind_param() 方法的数组转换为通过引用传递即可:
call_user_func_array(array($stmt, 'bind_param'), refValues($params)); 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; }