Why am I getting the error "Commands are not synchronized, you cannot run this command now"

Error documentation mentioned in title

If you get Teams are not synchronized; you cannot run this command now in your client code, you are calling client functions in the wrong order.

This can happen, for example, if you use mysql_use_result () and try a new query before you call mysql_free_result (). This can also happen if you try to execute two queries that return data without calling mysql_use_result () or mysql_store_result () between them.

From here: http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html

But in the first query, I am not retrieving data from the mysql database, I am just pasting. And in the second query, I get data from the 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); 

Up to this step, everything is fine. But when I execute the following query, it gives an 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 he gives the error Commands out of sync; you can't run this command now Commands out of sync; you can't run this command now . I can not understand this situation.

Note. There is some problem in the request, I executed the same request directly in PHPMyAdmin and it works fine.

+6
source share
1 answer

The query expects the result:

mysqli_multi_query ($ connection, $ query);

You need to use / save the result before you can continue with the following query: Since you look as if you do not really care about the first set of results, do it after multiple requests.

 do { $result = mysqli_store_result($connection); mysqli_free_result($result); }while(mysqli_next_result()); 

Another option is to close the connection and restart it.

 mysqli_close($connection); $connection = mysqli_connect("localhost","username","password","tbl_msgs"); 

It all depends on your requirements.

+13
source

Source: https://habr.com/ru/post/944123/


All Articles