php 为什么我收到错误“命令不同步;您现在无法运行此命令”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16339628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why I am getting the Error "Commands out of sync; you can't run this command now"
提问by Munib
The Documentation of the Error Mentioned in the Title Says
标题中提到的错误的文档说
If you get Commands out of sync; you can't run this command nowin 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.
如果命令不同步;您现在无法在客户端代码中运行此命令,您以错误的顺序调用客户端函数。
例如,如果您正在使用 mysql_use_result() 并尝试在调用 mysql_free_result() 之前执行新查询,则可能会发生这种情况。如果您尝试执行两个返回数据的查询而不调用 mysql_use_result() 或 mysql_store_result() ,也会发生这种情况。
From here: http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html
从这里: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.
但是在第一次查询中,我没有从 mysql 数据库中获取任何数据,我只是在插入。在第二个查询中,我从数据库中获取数据。
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
在这里它给出了 Error Commands out of sync; you can't run this command now
。我无法理解这种情况
Note: There is any Problem in the Query, I have executed the same query directly to PHPMyAdmin and it works fine.
注意:查询中有任何问题,我已经直接向 PHPMyAdmin 执行了相同的查询,并且运行良好。
回答by LightYearsBehind
There are result set pending from the query:
查询中有待处理的结果集:
mysqli_multi_query($connection,$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.
这一切都取决于您的要求。