首页 > 解决方案 > 使用 $this->db->count_all_results() 时出错;as 在布尔值上调用成员函数 num_rows()

问题描述

在我的模型中,我有一个基于 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 

错误有什么问题。谢谢

标签: phpcodeignitercodeigniter-3

解决方案


如果我认为您正在使用 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();
}

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


推荐阅读