[英]Commands out of sync; you can't run this command now while calling stored procedure in Mysql
[英]Commands out of sync; you can't run this command now in SugarCRM using mysql stored procedure
這是我的代碼如下:
public function listParkedAccounts($days = NULL)
{
global $db;
$days_db = (empty($days))?"7":$days;
$stmt = "CALL parked_accounts('.$days_db.')";
$result = $db->query($stmt,true,'Failed to fetch aged author record');
$aged_authors = mysqli_fetch_all($result,MYSQLI_ASSOC);
//mysql_free_result($result); tried this
$counter = 0;
$data = null;
foreach($aged_authors as $authors){
if(empty($authors['call_descp'])){
$stmt_notes = "SELECT description FROM notes WHERE description IS NOT NULL AND parent_id = '".$authors['call_id']."' AND parent_type = 'Calls' ORDER BY date_entered DESC LIMIT 1";
$notes_descp = $db->getOne($stmt_notes, TRUE, 'Error retrieving call notes');
$notes = (!empty($notes_descp))?$notes_descp[0]['description']:"No Notes";
}else{
$notes = (!empty($authors['call_descp']))?$authors['call_descp']:"No Notes";
}
$lead = 'stuff';
$data[$counter]['lead_id'] = $lead;
$data[$counter]['call_notes'] = $notes;
$counter++;
}
$size = sizeof($data);
$json_vals['draw'] = "1";
$json_vals['recordsTotal'] = $size;
$json_vals['recordsFiltered'] = $size;
$json_vals['data'] = ($size > 0)?$data:array();
return $json_vals;
}
我的問題在這里是此錯誤消息:
檢索呼叫注釋時出錯查詢失敗:從注釋中選擇描述不為空,並且parent_id ='123'和parent_type ='Calls'ORDER BY date_entered DESC LIMIT 1:MySQL錯誤2014:命令不同步; 您現在不能運行此命令
從閱讀本文中我了解到的是,我需要釋放結果或存儲結果,但是當我嘗試這些結果時,仍然收到相同的錯誤消息。 盡管我不太確定應該在哪里釋放結果,或者什至沒有正確執行結果。 這是我第一次使用存儲過程。
當您調用存儲過程時,該存儲過程可能包含需要處理的多個結果集。 如果您只處理第一個結果集並繼續嘗試使用相同的連接執行另一個查詢,則將獲得
“命令不同步”
錯誤。
為了解決這個問題,您應該使用multi_query
而不是$db->query
+ mysqli_fetch_all
(順便說一句,不要將面向對象的樣式和過程樣式混合在一起)。
PHP文檔中顯示了如何處理帶有行的多個結果集的示例: http : //php.net/manual/en/mysqli.multi-query.php
我更改了以下幾行:
$notes_descpRes = $db->query($stmt_notes);
$notes_descp = $db->fetchByAssoc($notes_descpRes);
$notes = (!empty($notes_descp))?$notes_descp['description']:"No Notes";
以下是完整的代碼:
public function listParkedAccounts($days = NULL)
{
global $db;
$days_db = (empty($days))?"7":$days;
$stmt = "CALL parked_accounts('.$days_db.')";
$result = $db->query($stmt,true,'Failed to fetch aged author record');
$aged_authors = mysqli_fetch_all($result,MYSQLI_ASSOC);
//mysql_free_result($result); tried this
$counter = 0;
$data = null;
foreach($aged_authors as $authors){
if(empty($authors['call_descp'])){
$stmt_notes = "SELECT description FROM notes WHERE description IS NOT NULL AND parent_id = '".$authors['call_id']."' AND parent_type = 'Calls' ORDER BY date_entered DESC LIMIT 1";
$notes_descpRes = $db->query($stmt_notes);
$notes_descp = $db->fetchByAssoc($notes_descpRes);
$notes = (!empty($notes_descp))?$notes_descp['description']:"No Notes";
}else{
$notes = (!empty($authors['call_descp']))?$authors['call_descp']:"No Notes";
}
$lead = 'stuff';
$data[$counter]['lead_id'] = $lead;
$data[$counter]['call_notes'] = $notes;
$counter++;
}
$size = sizeof($data);
$json_vals['draw'] = "1";
$json_vals['recordsTotal'] = $size;
$json_vals['recordsFiltered'] = $size;
$json_vals['data'] = ($size > 0)?$data:array();
return $json_vals;
}
試試看。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.