Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

php

mysqli

Can't figure out, whats causing error Parameter 3 to mysqli_stmt::bind_param() expected to be a reference, value given in...

PDO $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); 

Also tried OOP

OOP $insert_stmt = $mysqli->prepare($query); array_unshift($params, $param_type); call_user_func_array(array($insert_stmt, 'bind_param'), $params); $insert_stmt->execute(); 

But same error, only that now Parameter 2 is causing problem.

So, what's wrong with $params? I need $params to be an array of values.

like image 710
woopata Avatar asked Apr 20 '13 13:04

woopata


People also ask

What is Mysqli Bind_param () function in PHP?

Then, have a look at the bind_param() function: $stmt->bind_param("sss", $firstname, $lastname, $email); This function binds the parameters to the SQL query and tells the database what the parameters are. The "sss" argument lists the types of data that the parameters are.

How do you bind variables in PHP?

The PDOStatement::bindParam() function is an inbuilt function in PHP that is used to bind a parameter to the specified variable name. This function bound the variables, pass their value as input, and receives the output value, if any, of their associated parameter marker.


1 Answers

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; } 
like image 190
bitWorking Avatar answered Sep 19 '22 22:09

bitWorking