prepared statement - What is the difference between the following to methods for bind_param PHP -
i'm getting on using singleton approach , starting prepared statements... i'm racking brain on why 1 version of works , 1 not when, me, seem same thing... want work second way in order meet end goal.
this works:
call_user_func_array(array($stmt, 'bind_param'), array("i", 2));
this not:
$params = array("i", 2); call_user_func_array(array($stmt, 'bind_param'), $params);
you getting error message like
mysqli_stmt::bind_param() expected reference, value given in...
the problem bind_param()
in php 5.3+ requires array values reference while 5.2 works real values.
from docs:
care must taken when using mysqli_stmt_bind_param() in conjunction call_user_func_array(). note mysqli_stmt_bind_param() requires parameters passed reference, whereas call_user_func_array() can accept parameter list of variables can represent references or values (ref).
one solution create array of references
$params = array("i", 2); $tmp = array(); foreach($params $key => $value) { $tmp[$key] = &$params[$key]; } call_user_func_array(array($stmt, 'bind_param'), $tmp);
and similar solution is
function refvalues($arr){ if (strnatcmp(phpversion(),'5.3') >= 0) //reference required php 5.3+ { $refs = array(); foreach($arr $key => $value) $refs[$key] = &$arr[$key]; return $refs; } return $arr; } $params = array("i", 2); call_user_func_array(array($stmt, 'bind_param'), refvalues($params));
Comments
Post a Comment