1

Following my code:

$query = mysqli_prepare($res, $my_query);
foreach($data as $key => $value){
    $data[$key] = &$data[$key];
}
call_user_func_array(array($query, 'bind_param'), $data);
mysqli_stmt_execute($query);
mysqli_stmt_close($query);

The query works, but I found a warning in the log:

call_user_func_array() expects parameter 1 to be a valid callback

My PHP version is 5.5. How to solve it?

4
  • Just for reference, what does var_dump($query) give you? Commented Jul 28, 2014 at 2:48
  • @Darren bool(false), I found two more warning, expects parameter 1 to be mysqli_stmt boolean given in mysqli_stmt_execute and mysqli_stmt_close, but my query works. Commented Jul 28, 2014 at 2:51
  • You need to pass the mysqli object to the call_user_func_array Commented Jul 28, 2014 at 2:54
  • Looks like $query should normally be a mysqli statement object, and bind_param is a valid method of that object. Since $query == false then mysqli_prepare failed? Commented Jul 28, 2014 at 2:55

2 Answers 2

2
call_user_func_array([ClassName, Method], array $arguments);

The problem is that parameter 1 is an object in your case.

This should work

call_user_func_array(array('mysqli_stmt', 'bind_param'), $data);

Futhermore, mysqli_prepare returns false on error, so that would not be callable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, but I have some questions. How to use call_user_func_array(array('mysqli_stmt', 'bind_param'), $data); with my query? Replacing call_user_func_array(array($query, 'bind_param'), $data); with call_user_func_array(array('mysqli_stmt', 'bind_param'), $data); my query doesn't work...
Your query is failing then. Make sure error reporting is one. error_reporting(-1);
0

The call to mysqli_prepare() is likely failing, returning a boolean. You need to check to see if mysqli_prepare() actually returns a mysqli_stmt object before trying to use it.

$query = mysqli_prepare($res, $my_query);
if (!$query) {
    throw new Exception('Error in preparing statement: ' . mysqli_error($res));
}
foreach($data as $key => $value){
    $data[$key] = &$data[$key];
}
call_user_func_array(array($query, 'bind_param'), $data);
mysqli_stmt_execute($query);
mysqli_stmt_close($query);

Without this check, $query may be a boolean, which will of course not create a valid callback when used in the array passed to call_user_func_array().

Furthermore, I would also recommend using the OO style when utilizing mysqli.

$connection = new mysqli('host', 'username', 'password');

$statement = $connection->prepare($my_query);
if (!$statement) {
    throw new Exception('Error in preparing statement: ' . $connection->error);
}
foreach($data as $key => $value){
    $data[$key] = &$data[$key];
}
call_user_func_array([$statment, 'bind_param'], $data);
$statement->execute();
$statement->close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.