使用 $this->db->count_all_results() 时出错;作为对布尔值的成员函数 num_rows() 的调用
Error for using $this->db->count_all_results(); as Call to a member function num_rows() on boolean
在我的模型中,我有一个根据 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
错误是什么问题。谢谢
为什么不用$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')
放在最上面,我希望它也能正常工作。
如果我猜你使用的是 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();
}
如果没有引起我的注意,我希望这对您有所帮助
在我的模型中,我有一个根据 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
错误是什么问题。谢谢
为什么不用$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')
放在最上面,我希望它也能正常工作。
如果我猜你使用的是 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();
}
如果没有引起我的注意,我希望这对您有所帮助