加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 为什么我收到错误“命令不同步;你现在不能运行这个命令“

发布时间:2020-12-13 16:36:14 所属栏目:PHP教程 来源:网络整理
导读:标题中提到的错误文档说明 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 e
标题中提到的错误文档说明

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.

从这里:http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html

但是在First Query中我没有从mysql数据库中获取任何数据,我只是插入.在第二个查询中,我从数据库中获取数据.

这是我的代码

$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);

每一件事情都很好.但是当我执行以下查询时它会给出错误

$select_query = "SELECT * FROM msgs WHERE msg_id = {$msg_id}";

$result_set = mysqli_query($connection,$select_query);

if(!$result_set) {
    die(mysqli_error($connection)); 
}

这里它使错误命令不同步;你现在不能运行这个命令.我无法理解这种情况

注意:查询中有任何问题,我已经直接向PHPMyAdmin执行了相同的查询,并且工作正常.

查询中有待处理的结果集:

mysqli_multi_query($connection,$query);

您需要先使用/存储结果,然后才能继续下一个查询:
由于您看起来并不真正关心第一个结果集,请在多个查询之后执行此操作.

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

另一种方法是关闭连接并再次启动它.

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

这一切都取决于您的要求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读