2013-05-02 74 views
6

在標題中提到的錯誤的文件說,爲什麼我收到錯誤「命令不同步,你無法運行此命令現在」

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

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

從這裏:http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html

但在第一個查詢我不是從MySQL數據庫中獲取任何數據,我只是插入。在第二個查詢中,我從數據庫中獲取數據。

這裏是我的代碼

$connection = mysqli_connect("localhost","username","password","tbl_msgs"); 
if(mysqli_connect_errno($connection)) 
{ 
    die("Failed to connect to MySQL: " . mysqli_connect_error()); 
} 
$query = "INSERT INTO users (total_comments, total_views) 
      VALUES ({$total_comments}, {$total_views});"; 

$query .= "INSERT INTO msgs (notifications) VALUES ({$notifications})"; 

mysqli_multi_query($connection,$query); 

高達這個步驟每一件事情是罰款。但是,當我執行以下查詢它給人的錯誤

$select_query = "SELECT * FROM msgs WHERE msg_id = {$msg_id}"; 

$result_set = mysqli_query($connection,$select_query); 

if(!$result_set) { 
    die(mysqli_error($connection)); 
} 

這給出了錯誤Commands out of sync; you can't run this command now。我無法理解這種情況

注意:查詢中有任何問題,我已經直接對PHPMyAdmin執行相同的查詢,並且工作正常。

回答

13

有從查詢未決結果集:

mysqli_multi_query($連接,$查詢);

您需要使用/存儲結果,然後才能與後下一個查詢進行: 因爲你的樣子,你真的不關心的第一個結果集,多查詢後做..

do 
{ 
    $result = mysqli_store_result($connection); 
    mysqli_free_result($result); 
}while(mysqli_next_result()); 

另一種方法是關閉連接並重新開始吧..

mysqli_close($connection); 
$connection = mysqli_connect("localhost","username","password","tbl_msgs"); 

這一切都取決於你的需求。

+2

爲什麼我需要用mysqli而不是mysql來做到這一點? – Fearghal 2014-02-19 23:53:49

+0

自PHP版本5.5.0起,mysql_ *函數已被棄用。 – LightYearsBehind 2014-02-21 01:02:23

相關問題