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

CodeIgniter执行MySQL查询后无法正确获取状态计数问题

问题根源

你的模型方法get_records调用了CodeIgniter的查询构造器方法count_all_results(),这个方法返回的是符合条件的条目数量(整数类型),不是查询结果集对象。你在控制器里执行$data->num_rows()相当于尝试对整数调用方法,代码会抛出致命错误,导致后续的HTML渲染逻辑直接中断,所以看不到输出。

解决方法

你只需要修改控制器里的判断逻辑,去掉对num_rows()的调用,直接判断整数值即可,修改后的控制器fetch_status方法代码如下:

function fetch_status(){
    $output ='';
    $startDate = '';
    $endDate = '';
    $this->load->model('crm/user_model');
    
    if($this->input->post('startDate')){
        $startDate = $this->input->post('startDate');
    }
    if($this->input->post('endDate')){
        $endDate = $this->input->post('endDate');
    }
    // $data是整数类型的统计值
    $data = $this->user_model->get_records($startDate,$endDate);
    
    $output .= '
    <div class="table-responsive">
        <table class="table table-bordered table-striped">
            <tr>
                <th>Draft</th>
            </tr>
    ';
    // 直接判断统计值是否大于0即可
    if($data > 0)
    {
        $output .= '
        <tr>
            <td>'.$data.'</td>
        </tr>
        ';
    }
    else
    {
        $output .= '<tr>
            <td colspan="7">No Data Found</td>
            </tr>';
    }
    $output .= '</table>';
    echo $output;
}

额外优化建议:你当前模型的where条件直接拼接用户输入的日期参数,存在SQL注入风险,可以改成CodeIgniter查询构造器的原生写法自动做参数转义,同时统计数量不需要写select('*'),查询构造器会自动忽略:

public function get_records($st_date,$end_date){
    $this->db->from('crm_listings');
    $this->db->where('DATE(added_date) >=', $st_date);
    $this->db->where('DATE(added_date) <=', $end_date);
    $this->db->where('status', 'D');
    return $this->db->count_all_results();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 06:27:03