繁体   English   中英

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

[英]In PHP, MySQL throws Commands out of sync error, while trying to use '$mysqli->multi_query'

在PHP中,当我第二次尝试使用$ mysqli-> multi_query时,它引发以下错误。

我看了看MySQL手册,并在SO上问了类似的问题,都建议使用$mysqli->use_result(); $mysqli->store_result(); $mysqli->free_result();

但没有一个解决问题。 任何想法,我可能会丢失。

当前输出- '命令不同步; 您现在不能运行此命令”

<?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'

?>

您必须获取所有结果-例如:

// 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

某些语言的某些SQL系统是“惰性”的 它们仅在您要求结果时发送查询(例如C#中的LINQ)。 也许PHP会这样做。 它阻止连接等待您获取结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM