duanbo6482 2013-07-10 11:38
浏览 141
已采纳

在PHP中,MySQL尝试使用'$ mysqli-> multi_query'时抛出命令不同步错误

In PHP, while I try to use $mysqli->multi_query second time, it throws me following error.

I looked at MySQL manual, and similar questions asked at SO, all suggest using either $mysqli->use_result(); or $mysqli->store_result(); or $mysqli->free_result();.

But none solves the problem. Any idea what I might be missing.

Current output - 'Commands out of sync; you can't run this command now'

<?php

$link = mysqli_connect('localhost', 'root', '', 'database');

$array1 = array(1 => 1000, 2 => 1564, 3 => 2646, 4 => 5462, 5 => 8974);
$array2 = array(23 => 1975, 24 => 3789, 25 => 4658, 26 => 5978, 27 => 6879);

$update1 = '';
foreach($array1 as $k => $v) {
    $update1 .= "UPDATE `ps_temp` SET `price` = {$v} WHERE `id` = {$k};";
}

$res = mysqli_multi_query($link, $update1);     // Table is updated
$error = mysqli_error($link);
echo 'Error 1 - '.$error.'<hr>';                // Output : Error 1 - 

$update2 = '';
foreach($array2 as $k => $v) {
    $update2 .= "UPDATE `ps_temp2` SET `price` = {$v} WHERE `id` = {$k};";
}

mysqli_multi_query($link, $update2);            // Table is not updated.
$error = mysqli_error($link);
echo 'Error 2 - '.$error.'<hr>';                // Output: Error 2 - 'Commands out of sync; you can't run this command now'

?>
  • 写回答

1条回答 默认 最新

  • douluolan9101 2013-07-10 13:43
    关注

    You have to fetch all results - for example:

    // here: first multi query
    
    // fetch all results
    while( mysqli_more_results($link) ){
        $result = mysqli_store_result($link);
        mysqli_next_result($link);
    }
    
    // here: second multi query
    

    Some SQL systems in some languages are "lazy". They send query only when you ask for results (for example LINQ in C#). Maybe PHP do the same. It blocks connection waiting for your result fetching.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?