计算DB中的行数

cab*_*ret 4 php mysql activerecord codeigniter codeigniter-2

我正在关注CI/jQuery的分页教程.在本教程中,他们通过执行以下操作获得总行数:

$config['total_rows'] = $this->db->count_all('tblUsers');

他们获得定义分页的用户总数.但是,他们得到了所有用户.在我的应用程序中,我只需要为其分配了特定值的用户,在本例中为"role".

我只需要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();
Run Code Online (Sandbox Code Playgroud)

给我以下数组:

Array ( [0] => Array ( [count(*)] => 2 ) )

但是,我似乎无法读出count(*)索引..

Message: Undefined index: count(*)

我希望这是有道理的.基本上我正在尝试做类似的事情..

$this->db->where('role', 1)->get('tblUsers')->count()

可悲的是,这不起作用:D

提前致谢.任何帮助表示赞赏.

rav*_*ren 5

您可以简单地为count(*)结果列设置别名:

select count(*) as number_of_entries from tblusers where role = '1'
Run Code Online (Sandbox Code Playgroud)

您现在可以使用number_of_entries结果数组中的键来访问所需的值.