“命令不同步;您现在无法运行此命令” - 由 mysqli::multi_query 引起
我通过 mysqli::multi_query 运行多次删除,它弄乱了下一个查询。抛出以下错误。
Error - SQLSTATE HY000.
Sql error: Commands out of sync; you can't run this command now
我是否需要以某种方式清除多重查询,以免它干扰我的下一个查询?出现这个错误的原因是什么?
这就是我运行多重查询的方式:
function deleteSomeTables($args) {
$sql = 'delete 1;delete another;';
if ($database->multi_query($sql)) {
return true;
} else {
return false;
}
}
我在 Windows 7 上使用最新版本的 Xampp
I am running multiple deletes through mysqli::multi_query
and it is messing up the next query in line. The following error is being thrown.
Error - SQLSTATE HY000.
Sql error: Commands out of sync; you can't run this command now
Do I need to somehow clear the multi query so it doesn't mess with my next query? What is the cause of this error?
And this is how I am running my multi query:
function deleteSomeTables($args) {
$sql = 'delete 1;delete another;';
if ($database->multi_query($sql)) {
return true;
} else {
return false;
}
}
I am using a recent version of Xampp on windows 7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过使用 mysqli::multi_query 您正在触发查询,但是在继续之前,您需要处理这些查询的结果。 文档页面描述了处理结果的各种方法。一旦处理完毕,您将能够很好地执行其他查询。
您遇到的错误消息实际上在 mysqli::query< 页面上得到了更好的解释/a> (尽管请记住,在这种情况下 mysqli::query 不会返回结果对象,因为您正在执行删除操作)。
您当然可以将 multi_query 更改为多个单个查询,我不知道每种方法的优缺点是什么。
By using mysqli::multi_query you are firing off queries, but you need to handle the results of those queries before you move on. The documentation page describes the various ways of handling the results. Once handled, you will be able to perform other queries fine.
The error message you are encountering is actually explained a little better on the page for mysqli::query (although bear in mind that mysqli::query would not return a result object in this instance, as you are doing a delete).
You could of course change multi_query to multiple single queries, I don't know what the pros/cons of each approach are.
这帮助我删除了“命令不同步”错误:
在调用 multi_query 之后添加此代码,它将使用结果集并解决错误。
This helped me to remove 'Commands out of sync' error:
Add this code just after calling multi_query, it will use the result sets and resolve the error.
Seva 的代码很可能会解决你的问题,如果你像我一样使用程序风格,请使用这个
Seva's code will most likely fix your issue, if you're using procedural style like me, use this