2012-04-25 27 views
0

我知道類似的問題已經被問到,我嘗試了一堆(包括PHP文檔),但我似乎沒有得到這個工作。mysqli multi_query和insert_id讓我的命令不同步;

我有以下幾點:

$query = "insert into page title values ('v1');insert into page title values ('v2');insert into page title values ('v3');" 

$mysqlidb->multi_query($query); 

$index = 0; 
$insertId = $this->db->insert_id; 
for($i = $insertId; $i < $insertId + sizeof($pages); $i++) { 
    //store ids in some array 
} 

如果我執行上面的代碼,不管是什麼我嘗試下在DB做我得到「命令不同步,你現在不能運行這個命令「錯誤。

我明白我必須關閉所有結果,問題是我不知道如何。

似乎有結果,因爲$mysqli->more_results()返回true,但如果我然後調用use_result()或store_result()這些返回null。我試着在這之前並調用next_result後(),它總是空:

while($mysqli->more_results()) { 
    //next line returns error, calling close() on null: 
    $mysqli->use_result()->close(); 
    $mysqli->next_result(); 
} 

while($mysqli->more_results()) { 
    $mysqli->next_result(); 
    //next line returns error, calling close() on null: 
    $mysqli->use_result()->close(); 

} 

請幫幫忙,如何擺脫「命令不同步,你可以」現在執行這個命令「在執行multi_query插入並調用insert_id之後。謝謝。

回答

1
$values = ('v1' => '','v2' => '','v3' => ''); 
foreach ($values as $key => $val) { 
    $query = "insert into page title values ('$key')"; 
    $mysqlidb->query($query); 
    $values[$key] = $mysqlidb->insert_id; 
} 
+0

謝謝,但如果我用多個(單個)查詢替換multi_query,它會不會影響性能? – 2012-04-25 15:25:33

+0

不是。它不會 – 2012-04-25 16:23:55

+0

正確的你,我試過你的解決方案,我會用這個。謝謝。 – 2012-04-26 10:51:16

相關問題