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.

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?