2012-04-30 39 views
0

命令不同步執行此代碼時發生錯誤。Mysqli準備錯誤:命令不同步;

foreach ($groupsId as $gpId) { 

      $stmt = $db->query("CALL addUserToGroup(?,?)", array($userId, $gpId)); 
     $stmt->execute(); 


} 

此錯誤發生

Db_Statement_Mysqli_Exception' with message 'Mysqli prepare error: Commands out of sync; you can't run this command now' in /var/www/html/zend/Zend/Db/Statement/Mysqli.php:77 Stack trace: #0 

這裏的存儲過程

DELIMITER // 
CREATE PROCEDURE addUserToGroup(IN groupId INT(11),IN userId INT(11)) 

    BEGIN 
     insert into `group_users`(`group_id`,`user_id`) values(groupId ,userId); 
    END // 

DELIMITER ; 

,我應該怎麼辦???

+0

沒有想過工作?您的數據是否被驗證爲正確類型?這是一個非常簡單的準備好的語句,雖然它看起來像你的變量被向後傳遞。數組中的'$ gpId'不應位於'0'位置? – RockyFord

+0

我在本地嘗試,它工作正常,但當我在線工作時發生錯誤。 – palAlaa

+0

我需要這個問題的解決方案請:( – palAlaa

回答

0

一些這樣的事

foreach ($groupsId as $gpId) { 

    $stmt = $db->query("CALL addUserToGroup(?,?)", array($userId, $gpId)); 
    $result = $stmt->execute(); 
    //or $result = $query->fetchAll(); 
    $stmt->closeCursor(); 
} 

應該工作:)