2014-05-19 76 views
2

當運行下面的代碼,所述第二環路(它循環的次數的量的量)它給我一個命令不同步的錯誤。這是什麼造成的?命令不同步;你現在不能運行這個命令; PHP While循環

PHP代碼:

while ($i < $quantity) { 
    $result = mysqli_multi_query($con, " 
     INSERT INTO parts (capacity, length, width, height, orientation, weight, location, description, status, partNumber) VALUES ('$capacity', '$length', '$width', '$height', '$orient', '$weight', '$location', '$desc', '$status', '$partNum'); 

     INSERT INTO craneparts (craneID, partsID) VALUES ('$craneID', LAST_INSERT_ID()); 
    ") or die(mysqli_error($con)); 
    $i++; 
} 
+0

哇!沒有意識到有一個'mysqli_multi_query' ...太多學習 – asprin

+0

我知道!我今天才知道這件事。直到我發現需要它時,纔會考慮這種可能性。 – LiggyRide

回答

3

https://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html

如果你得到命令不同步;您現在不能在您的客戶端代碼 中運行此命令,而是按錯誤順序調用客戶端功能。

例如,如果您正在使用mysql_use_result()和 嘗試在調用mysql_free_result()之前執行新查詢,則可能發生這種情況。 如果您嘗試執行兩個查詢,如果沒有調用了mysql_use_result()或了mysql_store_result()之間返回數據 它也可能發生。

嘗試調用您的mysqli_multi_query之間mysqli_next_result調用

while ($i < $quantity) { 
    $result = mysqli_multi_query($con, " 
     INSERT INTO parts (capacity, length, width, height, orientation, weight, location, description, status, partNumber) VALUES ('$capacity', '$length', '$width', '$height', '$orient', '$weight', '$location', '$desc', '$status', '$partNum'); 

     INSERT INTO craneparts (craneID, partsID) VALUES ('$craneID', LAST_INSERT_ID()); 
    ") or die(mysqli_error($con)); 

    while(mysqli_more_results($con) && mysqli_next_result($con)) {;} // flush multi_queries 

    $i++; 
} 
+0

得到這個錯誤 'mysqli_free_result()期望參數1是mysqli_result' – LiggyRide

+0

@LiggyRide看起來像mysqli_free_result不是正確的路要走,因爲mysqli_multi_query沒有返回結果對象。你可以用mysqli_next_result()再試一次嗎? – FuzzyTree

+0

'mysqli_next_result()期望參數1爲mysqli,布爾給定'布爾值與mysqli_free_result()中的相同,只是將該部分剪掉。 – LiggyRide

相關問題