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

CodeIgniter函数报错:在布尔值上调用num_rows()方法求助

Troubleshooting "Call to a member function num_rows() on boolean" in CodeIgniter

Hey there, let's break down why you're hitting this error and how to fix it. That message means your database query is failing and returning false instead of a valid query object—so when you try to call num_rows() on it, PHP throws an error. Here's your step-by-step troubleshooting guide:

1. First, Grab the Exact Database Error

The fastest way to diagnose the issue is to check what's going wrong with your generated SQL. Add error checking right after you run the get() method:

$query = $this->ci->db->get($this->table, null, null, false);
if (!$query) {
    // Pull the database error details
    $db_error = $this->ci->db->error();
    // Log or print the error for debugging (remove this in production!)
    log_message('error', "DB Error: {$db_error['message']} | Generated SQL: {$this->ci->db->last_query()}");
    // For quick debugging, you can also print directly:
    // print_r($db_error);
    // echo $this->ci->db->last_query();
}

This will show you exactly what's broken—like a syntax error, missing table/column, or invalid join condition.

2. Fix the distinct() Usage

Your current distinct logic is likely causing invalid SQL. In CodeIgniter 3, the distinct() method doesn't accept a parameter—it's just a toggle to enable DISTINCT on all selected columns. If you want to apply DISTINCT to specific columns, include it directly in your select() call:

if (strlen($this->distinct) > 0) {
    // Use DISTINCT directly in the select statement
    $this->ci->db->select("DISTINCT {$this->columns}");
} else {
    $this->ci->db->select($this->columns);
}

Calling distinct($this->distinct) generates malformed SQL, which makes the query fail and return false.

3. Validate Your Query Builder Variables

Double-check that all class variables you're using are properly set up:

  • $this->joins: Each entry must be an array with 3 elements: [table_name, join_condition, join_type] (e.g., ['users', 'users.id = posts.user_id', 'left']). Missing elements here will break the join() call.
  • $this->table: Ensure this is a valid, existing table name (no typos!).
  • $this->where/$this->or_where: Each entry should reference real columns from your tables/joins—avoid typos or non-existent fields.

4. Check the get() Method Parameters

If you're using CodeIgniter 3, the get() method only accepts 3 parameters: ($table = '', $limit = null, $offset = null). Your fourth parameter (false) is invalid and may cause unexpected behavior. Remove it:

$query = $this->ci->db->get($this->table, null, null);

If you're on CodeIgniter 4, the get() method works differently—specify the table first with table():

$query = $this->ci->db->table($this->table)->get();

Final Notes

Once you add the error logging, you'll see the exact SQL being generated and the database's error message—that's the key to fixing this quickly. Most often, issues like this come from small syntax mistakes in your query builder setup (like the distinct issue) or missing/invalid table/column references.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:58:59