【问题标题】:Call to a member function num_rows() on bool在 bool 上调用成员函数 num_rows()
【发布时间】:2019-12-29 14:06:53
【问题描述】:

访问页面时发生错误并显示在页面上。显示的错误如下所述:

An uncaught Exception was encountered

Type: Error

Message: Call to a member function num_rows() on bool

Filename: localhost/app/models/admin/Reports_model.php

错误中引用的方法是这样的:

public function getTotalReturnSales($start, $end, $warehouse_id = null)
    {
        $this->db->select('count(id) as total, sum(COALESCE(grand_total, 0)) as total_amount, SUM(COALESCE(paid, 0)) as paid, SUM(COALESCE(total_tax, 0)) as tax', false)
            ->where('date BETWEEN ' . $start . ' and ' . $end);
        if ($warehouse_id) {
            $this->db->where('warehouse_id', $warehouse_id);
        }
        $q = $this->db->get('returns');
        if ($q->num_rows() > 0) {
            return $q->row();
        }
        return false;
    }

这个错误是怎么引起的?

【问题讨论】:

    标签: php mysql codeigniter model codeigniter-3


    【解决方案1】:

    如果查询失败,$this->db->get() 将返回 false。如您所知,false 是一个布尔值,没有“方法”。如果你运行以下

    $q = $this->db->get('returns');
    var_dump($q)
    

    它几乎肯定会告诉你 $q 是一个值为 false 的布尔值。

    查询失败的最常见原因是 sql 语句语法错误。查看查询生成器创建的查询语句的一种方法是将以下两行放在$q = $this->db->get('returns');之前

    $sql = $this->db->get_compiled_select('returns', false); 
    var_dump($sql);
    

    您也许能够看到导致问题的原因。

    注意:不返回任何行的查询不是失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      相关资源
      最近更新 更多