这是我的代码:
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;
}
我的问题是这个错误信息:
检索呼叫说明时出错查询失败:SELECT说明FROM 注意WHERE描述不是NULL而且parent_id = '123'和parent_type ='调用'ORDER BY date_entered DESC LIMIT 1:MySQL错误2014:命令不同步; 你现在不能运行这个命令
我从阅读this中理解的是,我需要释放结果或存储它们,但是当我尝试这些时,我仍然得到相同的错误消息。虽然我不太确定我应该在哪里释放结果,或者我是否正确地执行它们。这是我第一次使用存储过程。
答案 0 :(得分:1)
调用存储过程时,存储过程可能包含需要处理的多个结果集。如果您只处理第一个结果集并继续尝试使用相同的连接执行另一个查询,那么您将获得
“命令不同步”
错误。
要解决此问题,请使用$db->query
而不是mysqli_fetch_all
+ multi_query
(btw尽量不要混合面向对象的样式和程序样式)。
PHP文档中显示了如何使用行处理多个结果集的示例: http://php.net/manual/en/mysqli.multi-query.php
答案 1 :(得分:0)
我改变了以下几行:
$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;
}
试一试。