Search code examples
phpcodeignitercodeigniter-3

Error for using $this->db->count_all_results(); as Call to a member function num_rows() on boolean


In my model, I have a function for getting number of products based on 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();
}

I am getting error as

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 

What is the problem for error. Thank you


Solution

  • If i will gause you are using Codeigniter 4. If that is true. Please use this below. I think it will help you solve this issue. If you are extending to Codeigniter core model there is no need for $this->db. Just

    Extending to Codeigniter Core Model, Use

    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();
    }
    

    If not extending to Codeigniter Core model, use

    
    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();
    }
    

    If you are using codeigniter 3 as you said use this

    
    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();
    }
    

    OR

    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();
    }
    

    I hope this help if not call my attension