The Documentation of the Error Mentioned in the Title Says
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
But In First Query I am not fetching any data from mysql database, I am just inserting. And In second Query I am getting the data from database.
Here is My code
$connection = mysqli_connect("localhost","username","password","tbl_msgs");
if(mysqli_connect_errno($connection))
{
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$query = "INSERT INTO users (total_comments, total_views)
VALUES ({$total_comments}, {$total_views});";
$query .= "INSERT INTO msgs (notifications) VALUES ({$notifications})";
mysqli_multi_query($connection,$query);
Upto this Step every thing is fine. But When I execute the following query It gives the Error
$select_query = "SELECT * FROM msgs WHERE msg_id = {$msg_id}";
$result_set = mysqli_query($connection,$select_query);
if(!$result_set) {
die(mysqli_error($connection));
}
Here it gives the Error Commands out of sync; you can't run this command now
. I can't understand this situation
Note: There is any Problem in the Query, I have executed the same query directly to PHPMyAdmin and it works fine.
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.
来源:https://stackoverflow.com/questions/16339628/why-i-am-getting-the-error-commands-out-of-sync-you-cant-run-this-command-now