You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter报错Commands out of sync,无法连续执行查询求助

CodeIgniter "Commands out of sync; you can't run this command now" 报错解决求助

花费数小时尝试解决CodeIgniter下的Commands out of sync; you can't run this command now报错,场景是需要连续执行多个查询,且无法修改/system/database/drivers/mysqli/mysqli_result.php文件。

已尝试的方法

  • $this->db->reset_query();
  • $this->db->close(); + $this->db->initialize();
  • $this->db->reconnect();
  • mysqli_next_result( $this->db->conn_id );
  • $query->free_result();

以上方法要么仍报相同错误,要么触发其他问题:

  • 调用$this->db->free_result()提示Call to undefined method CI_DB_mysqli_driver::free_result()
  • 调用$result->free_result()提示Call to a member function free_result() on array

代码结构说明

有一个make_query方法,会根据传入的搜索选项决定关联的表和查询字段,支持执行结果计数、获取全量数据、查询唯一值或分组查询等操作。执行流程为先调用make_query,再确定查询字段,同时保存查询语句供用户查看。

特殊现象:当无查询选项(即SQL无WHERE子句)时,多个查询可以正常执行,不会触发报错。

相关代码

//Get rows from tblPlots based on criteria set in options array
public function get_plot_data($options=array()){
    $this->db->save_queries = TRUE;
    log_message('debug','before make query 1 get plot data');
    $this->make_query($options,'plot');    
    log_message('debug','after make query 1 get plot data'); 
    $this->db->distinct();
    
    //Now, set select to return only requested fields
    if (!empty($options['fields']) and $options['fields']!= "all" ){
        //Because other tables could be joined, add table name to each select
        array_walk($options['fields'], function(&$value, $key) { $value = 'tblPlots.'.$value;} );
        $this->db->select($options['fields']);
    }

    if (!empty($options['limit']) and $options['limit']>0){
        $this->db->limit($options['limit'], $options['offset']);
    }        
    //Get the resulting data
    $result=$this->db->get('tblProgram')->result();
    $query_text = $this->db->last_query();  //tried removing this but didn't help
    log_message('debug','query text '.$query_text);     
    $this->db->save_queries = FALSE;
    //get the number of rows
    //$this->db->reset_query();
    //$this->db->close();
    //$this->db->initialize();
    //$this->db->reconnect();
    //mysqli_next_result( $this->db->conn_id );
    //$this->db->free_result(); //Call to undefined method CI_DB_mysqli_driver::free_result()
    //$result->free_result(); //Call to a member function free_result() on array

    log_message('debug','before make query 2');
    $this->make_query($options,"plot");
    $this->db->select('pkProgramID');
    log_message('debug','before count results');
//this is where my code errors out trying to do the next step:
    $count=$this->db->count_all_results('tblProgram');
}

make_query代码较长且调用其他函数,暂未附上,但首次执行正常,能生成预期SQL。

疑问

不确定在使用Active Record构建查询时,如何正确调用free_result(),CI文档仅提供了使用原生query('SQL语句')的示例:

$query2 = $this->db->query('SELECT name FROM some_table');
$query2->free_result();

恳请提供解决方案!

内容的提问来源于stack exchange,提问作者xanabobana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 07:50:47