2014-01-10 73 views
0

我試圖用mysqli_multi_query()做多個更新,但它不起作用。可以用mysqli_multi_query做UPDATE語句嗎?

$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; 
+0

你能顯示你真正的查詢嗎? –

+0

陳述合成是正確的。我複製/過去在我的mysql工作臺生成的代碼,我看到沒有錯誤。 – Sebastien

+0

也許mysql服務器配置? – Sebastien

回答

-3

我找到了解決辦法,

我有一個MySQL錯誤「命令不同步,你可以在」現在運行這個命令「。在每個mysqli_multi_query之前和之後,我關閉mysqli連接,並開始新的連接。這個解決方案適用於我。

1

@ Sebastien,[cringe] ...你正在打開和關閉每個UPDATE查詢之間的mysqli連接?!?我很確定這不是最佳做法。

我有一個解決方案可以幫助你。當所有查詢語句返回true/false(無結果集)時,它使用mysqli_multi_query()。如果你不想使用它們,請移除mysqli_affected_rows()和mysqli_error()。讓我知道這是否有幫助。

Strict Standards: mysqli_next_result() error with mysqli_multi_query

+1

我已經提高了這一點,因爲我覺得它理智和正確。但是,我不認爲只爲了這個目的而在* mysqli_multi_query相關問題下留下你的答案是一個好主意。 –