Php 参考-我的codeigniter代码有什么问题

Php 参考-我的codeigniter代码有什么问题,php,codeigniter,codeigniter-3,codeigniter-2,Php,Codeigniter,Codeigniter 3,Codeigniter 2,大家好,我写了一个codeigniter函数从数据库返回数据 这是我的职责 public function get_total_results($filtering = false) { if ($filtering) { $this->get_filtering(); } foreach ($this->joins as $val) { $this->ci->db->join($val[0], $val

大家好,我写了一个codeigniter函数从数据库返回数据 这是我的职责

  public function get_total_results($filtering = false)
{
    if ($filtering) {
        $this->get_filtering();
    }

    foreach ($this->joins as $val) {
        $this->ci->db->join($val[0], $val[1], $val[2]);
    }

    foreach ($this->where as $val) {
        $this->ci->db->where($val[0], $val[1], $val[2]);
    }

    foreach ($this->or_where as $val) {
        $this->ci->db->or_where($val[0], $val[1], $val[2]);
    }

    foreach ($this->group_by as $val) {
        $this->ci->db->group_by($val);
    }

    foreach ($this->like as $val) {
        $this->ci->db->like($val[0], $val[1], $val[2]);
    }

    if (strlen($this->distinct) > 0) {
        $this->ci->db->distinct($this->distinct);
        $this->ci->db->select($this->columns);
    }

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

    return $query->num_rows();
}
但是我得到了一个错误

遇到未捕获的异常 类型:错误

消息:在布尔值上调用成员函数num_rows()

return$query->num_rows()行中
我不知道我的代码出了什么问题,我得到了这个错误,所以我知道返回结果的最后一行中的错误任何建议或想法codeigniter的get()方法的语法如下:

get([$table=''[,$limit=NULL[,$offset=NULL]])

参数:$table(string)–要查询的表$limit(int)– 限额条款$offset(int)–抵销条款

返回:CI_DB_结果实例(方法链接)

返回类型:CI\U DB\U结果

您传递了三个以上的参数。所以,请检查参数。这可能会解决你的问题


有关更多信息,请参阅。

问题在于线路

$query = $this->ci->db->get($this->table, null, null, false);
// Note: all you really need is $this->ci->db->get($this->table);
它将值
false
分配给
$query
。当查询生成器创建一条毫无意义的SQL语句时,通常会发生这种情况——它会发生

您可以看到查询生成器以这种方式创建的内容

// comment out the get() call
// $this->ci->db->get($this->table);
// run this instead
$sql = $this->db->get_compiled_select($this->table);
echo $sql;
// remove the comments once you see where the problem is
//return $query->num_rows();
您可能会看到SQL语句语法错误的地方,并相应地调整早期代码

您可能希望添加对
get()
返回到逻辑的检查,例如

$query = $this->ci->db->get($this->table);
// Is $query truthy? (not false, null, etc)
if(! empty($query))
{
    return $query->num_rows();
}

请参阅创建布尔值上的。
num_rows()
之前在这里已经多次提到过。本质上,这意味着您的查询失败,例如返回false,因此无法对对象执行更多操作
num_rows()
result()
row()
等。在database.php中打开
db_debug
,找出错误消息。另外,请避免提出“为什么我的代码不起作用”的问题。它实际上是一个关闭选项的名称。首先,我需要问一下为什么有
$this->ci->db->join()
而不是
$this->db->join()
?其次,如果您正在使用
方法链接
连接()
,那么
从哪里来()
?它真的没有任何效果-php不关心您是否将超过必要的参数传递给函数。。。