计算数据库中的行数,其中
我正在学习有关 CI/jQuery 分页的教程。在本教程中,他们通过执行以下操作来获取总行数:
$config['total_rows'] = $this->db->count_all('tblUsers');
他们获取用户总数来定义分页。然而,他们获得了所有用户。在我的应用程序中,我只需要为用户分配特定的值,在本例中为“角色”。
我只需要数据库中 role = 1
的用户。
我已经尝试使用 ->count()
(来自 CI 的 Active Record DB 类)或尝试 count()
来检查有多少“行”一个数组有,但我无法得到我需要的结果。我还尝试进行常规查询: select count(*) from tblusers where role = 1
然后以某种方式尝试获取它返回的数量,但是可惜。
这样做
$config['total_row'] = $this->db->query("select count(*) from tblusers where role = '1'")->result_array();
给了我以下数组:
Array ( [0] => Array ( [count(*)] => 2 ) )
但是,我似乎无法读出count(*)
index..
消息:未定义索引:count(*)
我希望这有意义。基本上我正在尝试做类似的事情..
$this->db->where('role', 1)->get('tblUsers')->count()
遗憾的是,这不起作用:D
提前致谢。任何帮助表示赞赏。
I'm following a tutorial on Pagination with CI/jQuery. In the tutorial they get the total number of rows by doing:
$config['total_rows'] = $this->db->count_all('tblUsers');
They get the total number of users to define the pagination. However, they get ALL users. In my application, I only need the users with a certain value assigned to them, in this case 'role'.
I only need the users where role = 1
in my DB.
I've tried a couple of things with ->count()
(from CI's Active Record DB class) or trying to count()
to check how many 'rows' an array has, but I haven't been able to get the result I need. I also tried doing a regular query: select count(*) from tblusers where role = 1
and then somehow tried grabbing how many it returned, but alas.
Doing
$config['total_row'] = $this->db->query("select count(*) from tblusers where role = '1'")->result_array();
gives me the following array:
Array ( [0] => Array ( [count(*)] => 2 ) )
However, I can't seem to be able to read out the count(*)
index..
Message: Undefined index: count(*)
I hope this makes some sense. Basically I'm trying to do something like..
$this->db->where('role', 1)->get('tblUsers')->count()
Sadly, this doesn't work :D
Thanks in advance. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以简单地为
count(*)
结果列添加别名:您现在可以使用
number_of_entries
作为结果数组中的键来访问所需的值。You could simply alias the
count(*)
result column:You can now use
number_of_entries
as the key from the resulting array to access the needed value.不是积极的,但似乎您所需要的只是为该列命名。例如,
我怀疑这会起作用。
Not positive, but it seems that all you need is to give that column a name. For example,
I suspect that will work.
要读取 count(*) 索引,请使用以下命令创建一个可读字段:
现在您将拥有:
For reading the count(*) index use the following to create a readable field:
Now you will have: