Commands out of sync; you can't run this command now
You can't have two simultaneous queries because mysqli uses unbuffered queries by default (for prepared statements; it's the opposite for vanilla mysql_query
). You can either fetch the first one into an array and loop through that, or tell mysqli to buffer the queries (using $stmt->store_result()
).
See here for details.
MySqli Commands out of sync; you can't run this command now
If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.
This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.
From here:
http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html
Update
If you make the a variable for the query and paste the variable directly into something like MySQL Workbench you can check the syntax prior to execution.
<?php
function myConnection(){
$myConnection = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
return $myConnection;
}
function register_user($register_data) {
array_walk($register_data, 'array_sanitize');
//Make the array readable and seperate the fields from data
$fields = '`' . implode('`, `', array_keys($register_data)) . '`';
$data = "'" . implode("', '", $register_data) . "'";
//Insert the data and email an activation email to the user
$query = "INSERT INTO `members` ($fields) VALUES ($data)";
$myNewConnection = myConnection();
if($result = mysqli_query($myNewConnection, $query)){
email($register_data['mem_email'], 'Activate your account', "Hello " . $register_data['mem_first_name'] . ",\n\nThank you for creating an account with H Fencing. Please use the link below to activate your account so we can confirm you identity:\n\nhttp://blah.blah.co.uk/activate.php?mem_email=" . $register_data['mem_email'] . "&email_code=" . $register_data['email_code'] . "\n\n - David & Jay ");
mysqli_free_result($result);
return ("Success");
} else {
echo $query;
die(mysqli_error($myNewConnection));
}
}
?>
Why I am getting the Error Commands out of sync; you can't run this command now
There are result set pending from the query:
mysqli_multi_query($connection,$query);
You need to use/store result before you can proceed with next query after:
Since you look like you don't really care about the first result set, do this after the multi query..
do
{
$result = mysqli_store_result($connection);
mysqli_free_result($result);
}while(mysqli_next_result());
Another alternative is to close the connection and starts it again..
mysqli_close($connection);
$connection = mysqli_connect("localhost","username","password","tbl_msgs");
It all depends on your requirements.
I get: Commands out of sync; you can't run this command now?
This is what to do, if I've understood the other answer correctly:
if ($link->multi_query($sql) === TRUE) {
// the loop below is the fix. It just cycles through the results, ignoring them
while(mysqli_more_results($link)) {
mysqli_next_result($link);
// or $link->next_result();
}
//...
Related Topics
How to Use Multiple Databases in Laravel
Convert PHP Array to JavaScript
A Simple Program to Crud Node and Node Values of Xml File
How to Display HTML Tags as Plain Text
Search Form With One or More (Multiple) Parameters
PHP Elvis Operator VS Null Coalescing Operator
Invoking a PHP Script from a MySQL Trigger
PHP Regular Expressions: No Ending Delimiter '^' Found In
How to Properly Add Cross-site Request Forgery (Csrf) Token Using PHP
How to Get the Jquery $.Ajax Error Response Text
When Should I Use 'Self' Over '$This'
Sending Email With PHP from an Smtp Server
How to Prevent Duplicate Usernames When People Register
How to Find Where I Will Be Redirected Using Curl in PHP