php - 使用 $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
错误有什么问题。谢谢
解决方案
如果我认为您正在使用 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();
}
如果不引起我的注意,我希望这会有所帮助
推荐阅读
- r - Colsum 新数据框
- node.js - 节点 12.14.1 端口问题。关闭服务器后也不占用端口
- .htaccess - 在 htaccess 中重定向以强制大写 URL
- php - 无法从php登录mysql
- java - @Inject 在无参数公共方法上
- css - 每第 N 个元素重复 CSS 样式
- c++ - CMake 在 Windows 上与 dll 链接 - 未定义的参考
- javascript - 使用 v-on:change 将特定对象元素传递给使用 Vue 和 Vuetify 的函数
- c# - 我可以通过网络流将位图文件从 csharp 应用程序发送到 android 应用程序吗?
- python - python sympy中对产品的期望