-2

尝试执行以下 PHP 代码,但是只有第一条语句成功执行,其余的则没有。

<?php

$link = mysqli_connect('localhost', 'root', '');
mysqli_select_db($link, 'database_name');


$update = " UPDATE `temp` SET `price` = 1000 WHERE `id` = 1;
            UPDATE `temp` SET `price` = 2000 WHERE `id` = 2;
            UPDATE `temp` SET `price` = 3000 WHERE `id` = 3;
            UPDATE `temp` SET `price` = 4000 WHERE `id` = 4;
            UPDATE `temp` SET `price` = 5000 WHERE `id` = 5;";

mysqli_multi_query($link, $update);

$update2 = "UPDATE `temp` SET `price` = 6000 WHERE `id` = 6;
            UPDATE `temp` SET `price` = 7000 WHERE `id` = 7;
            UPDATE `temp` SET `price` = 8000 WHERE `id` = 8;
            UPDATE `temp` SET `price` = 9000 WHERE `id` = 9;
            UPDATE `temp` SET `price` = 10000 WHERE `id` = 10;";

mysqli_multi_query($link, $update2);

?>

mysqli_multi_query($link, $update); -成功执行

mysqli_multi_query($link, $update2); -未执行。

任何人都可以指导,如果我可能遗漏了什么,或者关于语法。

4

4 回答 4

1

只使用一个查询怎么样

UPDATE `temp` 
SET `price` = case when id = 1 then 1000 
                   when id = 2 then 2000
                   when id = 3 then 3000
                   when id = 4 then 4000
                   when id = 5 then 5000
                   when id = 6 then 6000
                   when id = 7 then 7000
                   when id = 8 then 8000
                   when id = 9 then 9000
                   when id = 10 then 10000
                   else price
              end
于 2013-07-10T09:19:56.580 回答
0
$sql = "UPDATE temp SET price = ? WHERE id = ?";
$stm = $link->prepare($sql);
$link->autocommit(FALSE);
$stm->bind_param("ii" $row['price'], $row['id']);
foreach ($array as $row) {
    $stm->execute();
}
$link->commit();

像这样但未经测试的东西。我一直鄙视mysqli处理prepared statement的方式

于 2013-07-10T09:26:41.903 回答
0

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

// 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 也这样做。它阻止连接等待您的结果获取。

于 2013-07-10T15:03:44.567 回答
-2

您是否尝试将所有更新合并为一个然后运行它?

 $link = mysqli_connect('localhost', 'root', '');
 mysqli_select_db($link, 'database_name');


 $update = " UPDATE `temp` SET `price` = 1000 WHERE `id` = 1;
        UPDATE `temp` SET `price` = 2000 WHERE `id` = 2;
        UPDATE `temp` SET `price` = 3000 WHERE `id` = 3;
        UPDATE `temp` SET `price` = 4000 WHERE `id` = 4;
        UPDATE `temp` SET `price` = 5000 WHERE `id` = 5;
        UPDATE `temp` SET `price` = 6000 WHERE `id` = 6;
        UPDATE `temp` SET `price` = 7000 WHERE `id` = 7;
        UPDATE `temp` SET `price` = 8000 WHERE `id` = 8;
        UPDATE `temp` SET `price` = 9000 WHERE `id` = 9;
        UPDATE `temp` SET `price` = 10000 WHERE `id` = 10;";

  mysqli_multi_query($link, $update);
于 2013-07-10T09:26:08.417 回答