mysqli_multi_query可以执行UPDATE语句吗?

时间:2014-01-10 14:52:52

标签: php mysql mysqli

我尝试使用mysqli_multi_query()进行多项更新,但不起作用。

$arr_updates[] = "UPDATE MYTABLE SET COLUMN = 'VALUE' WHERE ID = 1";
$arr_updates[] = "UPDATE MYTABLE SET COLUMN = 'VALUE' WHERE ID = 2";

$b = mysqli_multi_query($CONN,join("; ",$arr_updates));

$b为FALSE,即使SQL语句的语法正确。

mysqli_multi_query()是否应该与MySQL UPDATE语句一起使用?或者有什么方法可以优化我的更新声明? (我不能使用switch语句。)

这些是生成的查询:

UPDATE cj_term_taxonomy SET parent = 0 WHERE parent = 120;
UPDATE cj_term_taxonomy SET parent = 1 WHERE parent = 121;
UPDATE cj_term_taxonomy SET parent = 2 WHERE parent = 122;
UPDATE cj_term_taxonomy SET parent = 3 WHERE parent = 123;
UPDATE cj_term_taxonomy SET parent = 4 WHERE parent = 124;
UPDATE cj_term_taxonomy SET parent = 5 WHERE parent = 125;
UPDATE cj_term_taxonomy SET parent = 6 WHERE parent = 126;
UPDATE cj_term_taxonomy SET parent = 7 WHERE parent = 127;
UPDATE cj_term_taxonomy SET parent = 8 WHERE parent = 130;
UPDATE cj_term_taxonomy SET parent = 9 WHERE parent = 131;
UPDATE cj_term_taxonomy SET parent = 10 WHERE parent = 132;
UPDATE cj_term_taxonomy SET parent = 11 WHERE parent = 133;
UPDATE cj_term_taxonomy SET parent = 12 WHERE parent = 134;
UPDATE cj_term_taxonomy SET parent = 13 WHERE parent = 135;
UPDATE cj_term_taxonomy SET parent = 14 WHERE parent = 136;
UPDATE cj_term_taxonomy SET parent = 15 WHERE parent = 137;
UPDATE cj_term_taxonomy SET parent = 16 WHERE parent = 138; 
UPDATE cj_term_taxonomy SET parent = 17 WHERE parent = 139;
UPDATE cj_term_taxonomy SET parent = 18 WHERE parent = 140;
UPDATE cj_term_taxonomy SET parent = 19 WHERE parent = 141;
UPDATE cj_term_taxonomy SET parent = 20 WHERE parent = 142;
UPDATE cj_term_taxonomy SET parent = 21 WHERE parent = 143;
UPDATE cj_term_taxonomy SET parent = 22 WHERE parent = 144;
UPDATE cj_term_taxonomy SET parent = 23 WHERE parent = 145;
UPDATE cj_term_taxonomy SET parent = 24 WHERE parent = 146;
UPDATE cj_term_taxonomy SET parent = 25 WHERE parent = 147;
UPDATE cj_term_taxonomy SET parent = 26 WHERE parent = 148;
UPDATE cj_term_taxonomy SET parent = 27 WHERE parent = 149;
UPDATE cj_term_taxonomy SET parent = 28 WHERE parent = 150;
UPDATE cj_term_taxonomy SET parent = 29 WHERE parent = 151;
UPDATE cj_term_taxonomy SET parent = 30 WHERE parent = 152;
UPDATE cj_term_taxonomy SET parent = 31 WHERE parent = 153;
UPDATE cj_term_taxonomy SET parent = 32 WHERE parent = 154;
UPDATE cj_term_taxonomy SET parent = 33 WHERE parent = 155;
UPDATE cj_term_taxonomy SET parent = 34 WHERE parent = 156;
UPDATE cj_term_taxonomy SET parent = 35 WHERE parent = 157;
UPDATE cj_term_taxonomy SET parent = 36 WHERE parent = 158;
UPDATE cj_term_taxonomy SET parent = 37 WHERE parent = 182;
UPDATE cj_term_taxonomy SET parent = 38 WHERE parent = 181;
UPDATE cj_term_taxonomy SET parent = 39 WHERE parent = 178;
UPDATE cj_term_taxonomy SET parent = 40 WHERE parent = 179;
UPDATE cj_term_taxonomy SET parent = 41 WHERE parent = 180;
UPDATE cj_term_taxonomy SET parent = 42 WHERE parent = 201;

2 个答案:

答案 0 :(得分:1)

@ Sebastien,[畏缩] ...你正在打开和关闭每个UPDATE查询之间的mysqli连接?!?我很确定这不是最佳做法。

我在其他地方有一个可以帮助你的解决方案。当所有查询语句都返回true / false(无结果集)时,它使用mysqli_multi_query()。如果您不想使用它们,请删除mysqli_affected_rows()和mysqli_error()。如果这有帮助,请告诉我。

Strict Standards: mysqli_next_result() error with mysqli_multi_query

答案 1 :(得分:-3)

我找到了解决方案,

我有一个mysql错误“命令不同步你现在无法运行此命令”。在每个mysqli_multi_query之前和之后,我关闭mysqli连接并启动新的连接。这个解决方案适合我。