【问题标题】:Error for using $this->db->count_all_results(); as Call to a member function num_rows() on boolean使用 $this->db->count_all_results() 时出错; as 在布尔值上调用成员函数 num_rows()
【发布时间】:2020-09-11 08:31:44
【问题描述】:

在我的模型中,我有一个根据 category_id 获取产品数量的函数

public function products_count($category_id, $subcategory_id = null, $brand_id = null)
{
    if ($category_id) {
        $this->db->where('category_id', $category_id);
    }
    if ($subcategory_id) {
        $this->db->where('subcategory_id', $subcategory_id);
    }
    if ($brand_id) {
        $this->db->where('brand', $brand_id);
    }
    $this->db->where('hide_pos !=', 1);
    $this->db->from('products');
    return $this->db->count_all_results();
}

我收到错误

An uncaught Exception was encountered

Type: Error

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

Filename: /var/www/html/projects/demo/system/database/DB_query_builder.php

Line Number: 1429

Backtrace:

File: /var/www/html/projects/demo/app/models/admin/Pos_model.php
Line: 158
Function: count_all_results 

错误是什么问题。谢谢

【问题讨论】:

    标签: php codeigniter codeigniter-3


    【解决方案1】:

    如果我认为你正在使用 Codeigniter 4。如果这是真的。请在下面使用它。我认为它会帮助你解决这个问题。 如果您要扩展到 Codeigniter 核心模型,则不需要 $this->db。只是

    扩展到 Codeigniter 核心模型,使用

    public function products_count($category_id, $subcategory_id = null, $brand_id = null)
    {
        $query = $this->table('products'); // self::table('products')
    
        if ($category_id) {
            $query = $query->where('category_id', $category_id);
        }
        if ($subcategory_id) {
            $query = $query->where('subcategory_id', $subcategory_id);
        }
        if ($brand_id) {
            $query = $query->where('brand', $brand_id);
        }
        $query = $query->where('hide_pos !=', 1);
        
        return $query->countAllResults();
    }
    

    如果不扩展到 Codeigniter Core 模型,请使用

    
    public function __construct()
    {
        $this->db = db_connect();
    }
    public function products_count($category_id, $subcategory_id = null, $brand_id = null)
    {
        $query = $this->db->table('products');
    
        if ($category_id) {
            $query = $query->where('category_id', $category_id);
        }
        if ($subcategory_id) {
            $query = $query->where('subcategory_id', $subcategory_id);
        }
        if ($brand_id) {
            $query = $query->where('brand', $brand_id);
        }
        $query = $query->where('hide_pos !=', 1);
        
        return $query->countAllResults();
    }
    

    如果您按照您所说的使用 codeigniter 3,请使用此

    
    public function products_count($category_id, $subcategory_id = null, $brand_id = null)
    {
        $query = $this->db->from('products');
    
        if ($category_id) {
            $query = $query->where('category_id', $category_id);
        }
        if ($subcategory_id) {
            $query = $query->where('subcategory_id', $subcategory_id);
        }
        if ($brand_id) {
            $query = $query->where('brand', $brand_id);
        }
        $query = $query->where('hide_pos !=', 1);
        
        return $query->count_all_results();
    }
    

    public function products_count($category_id, $subcategory_id = null, $brand_id = null)
    {
        $query = $this->db->from('products');
    
        if ($category_id) {
            $query = $query->where('category_id', $category_id);
        }
        if ($subcategory_id) {
            $query = $query->where('subcategory_id', $subcategory_id);
        }
        if ($brand_id) {
            $query = $query->where('brand', $brand_id);
        }
        $query = $query->where('hide_pos !=', 1)->get();
        
        return $query->num_row();
    }
    

    如果不引起我的注意,我希望这会有所帮助

    【讨论】:

    • 感谢您的回复,但我使用的是 ci3
    • 我已经编辑了我的帖子,所以请检查它是否有 codeigniter 3
    【解决方案2】:

    为什么不用$this->db->num_rows()而不是$this->db->count_all_results(),结果更准确,我正在重写你的函数,请试试这个。

    public function products_count($category_id, $subcategory_id = null, $brand_id = null)
    {
        $this->db->from('products');
        if ($category_id) {
            $this->db->where('category_id', $category_id);
        }
        if ($subcategory_id) {
            $this->db->where('subcategory_id', $subcategory_id);
        }
        if ($brand_id) {
            $this->db->where('brand', $brand_id);
        }
        $this->db->where('hide_pos !=', 1);
        $this->db->get();
        return $this->db->num_rows();
    }
    

    如果您想使用自己的代码,只需将$this->db->from('products') 放在顶部,我希望这也可以。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      • 2019-12-29
      • 2017-09-25
      • 1970-01-01
      • 2021-05-23
      • 2018-12-17
      • 2018-10-20
      相关资源
      最近更新 更多