问题描述
标题中提到的错误文档
例如,如果您使用的是mysql_use_result()和 在调用mysql_free_result()之前,尝试执行一个新查询. 如果您尝试执行两个返回数据的查询,也会发生这种情况 而不在两者之间调用mysql_use_result()或mysql_store_result().
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.
从此处: http://dev .mysql.com/doc/refman/5.0/en/commands-out-of-sync.html
但是在第一个查询中,我没有从mysql数据库中获取任何数据,我只是在插入.在第二次查询中,我从数据库中获取数据.
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.
这是我的代码
$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));
}
在这里给出错误Commands out of sync; you can't run this command now
.我无法理解这种情况
Here it gives the Error Commands out of sync; you can't run this command now
. I can't understand this situation
注意:查询中有任何问题,我已经直接对PHPMyAdmin执行了相同的查询,并且工作正常.
Note: There is any Problem in the Query, I have executed the same query directly to PHPMyAdmin and it works fine.
推荐答案
有待查询的结果集:
您需要使用/存储结果,然后才能继续下一个查询:由于您似乎并不真正在意第一个结果集,因此请在多次查询之后执行此操作.
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.
这篇关于为什么我出现“错误"命令不同步;您不能立即运行此命令"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!