MySQLi Bind_Param() Expected to Be a Reference, Value Given

mysqli bind_param() expected to be a reference, value given

UPDATE

This answer is outdated. Please use the spread operator in newer PHP versions like answered by Stacky.

From php docu:

Care must be taken when using mysqli_stmt_bind_param() in conjunction with call_user_func_array(). Note that mysqli_stmt_bind_param() requires parameters to be passed by reference, whereas call_user_func_array() can accept as a parameter a list of variables that can represent references or values.

And on the page mysqli-stmt.bind-param you have different solutions:

For example:

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;
}

Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given

Problem solved.

Resolve the issue:

instead $type = array('i','i'); need use $type = array('ii');

mysqli_stmt_bind_param - expects references

This line

mysqli_stmt_bind_param($stmt, $types, $ref);

means that you have one reference to bind.

Why? Let's see:

  • first argument is a statement
  • second argument is a string with types
  • following arguments are references to values which should be binded.

As you pass one argument (it is $ref) - you are trying to bind only one value. And $ref is not a reference, it is array of values which are refernces. See the difference? Array of references vs reference.

So, you took second approach, and it is a right one:

$values =  refValues($data);
call_user_func_array(array($stmt, 'bind_param'), $values);

What's the error here? You didn't pass types $types:

// do not assign anything to a variable
// pass results of `refValues` directly to `call_user_func_array`
call_user_func_array(array($stmt, 'bind_param'), array_merge(array($types), refValues($data)));

What do we do here: we are trying to call $stmt->bind_param and pass to this function arguments as array.

What are the arguments of $stmt->bind_param?

  • first argument is types ($types)
  • following arguments are references to values ($values)

Now it should work.

Pass by reference problem with PHP 5.3.1

You are passing an array of elements ($passArray). The second item inside the passed array needs to be a reference, since that is really the list of items you are passing to the function.

PHP: Value Expected To Be A Reference with Call_User_Func and Bind Params

if your php version is not outdated you can make it much simper

$params = implode(',', array_fill(0, count($greaterThan), '?'));
$stmt = $mysqli->prepare("SELECT nombre FROM usuarios WHERE id IN ($params)");
$types = str_repeat('i',count($greaterThan));
$stmt->bind_param($types, ...$greaterThan);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($nombresmayores);
$arraynombresmayores = array();
$stmt->store_result();
while($stmt->fetch()){
$arraynombresmayores[] = $nombresmayores;
}

but better yet use PDO:

$params = implode(',', array_fill(0, count($greaterThan), '?'));
$stmt = $pdo->prepare("SELECT nombre FROM usuarios WHERE id IN ($params)");
$stmt->execute($greaterThan);
$arraynombresmayores = $stmt->fetchAll(PDO::FETCH_COLUMN);

just compare this neat code with that awful mess you have now.

mysqli - mysqli_stmt::bind_param() expected to be a reference

$param looks like this right before you pass it to call_user_func_array:

[['ssi'],'mori','gre','25']

This is incorrect. You should change this:

array_unshift($param,array(DAL::paramtypez($param)));

To this:

array_unshift($param,DAL::paramtypez($param));

Errors appearing in mysqli code and call_user_func_array()

This is a sticky situation that is caused by changing of call_user_func_array behavior in PHP 5.4 (I have to assume): Documentation

As ugly as this is, it will work to call bind_param this way:

$selectedstudentanswerqry .= ' WHERE ' . implode(' AND ', $where);
global $mysqli;
$stmt =$mysqli->prepare($selectedstudentanswerqry);

if (count($where) === 1) {
$stmt->bind_param($parameterTypes, $parameters[0]);
}
else if (count($where) === 2) {
$stmt->bind_param($parameterTypes, $parameters[0], $parameters[1]);
}
else if (count($where) === 3) {
$stmt->bind_param($parameterTypes, $parameters[0], $parameters[1],
$parameters[2]);
}

I hate this as much as you probably do. I suggest switching from mysqli to PDO which handles variable parameters in a much nicer fashion (and has superior syntax in general, in my opinion):

$pdo = new PDO('mysql:host=localhost', 'username', 'password');
$stmt = $pdo->prepare($selectedstudentanswerqry);
$stmt->execute($parameters);
$selectedstudentanswernum = $stmt->rowCount();


Related Topics



Leave a reply



Submit