计算数据库中的行数,其中

发布于 2024-11-06 07:04:06 字数 974 浏览 0 评论 0原文

我正在学习有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛夏尉蓝 2024-11-13 07:04:06

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

select count(*) as number_of_entries from tblusers where role = '1'

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

You could simply alias the count(*) result column:

select count(*) as number_of_entries from tblusers where role = '1'

You can now use number_of_entries as the key from the resulting array to access the needed value.

尛丟丟 2024-11-13 07:04:06

不是积极的,但似乎您所需要的只是为该列命名。例如,

... = $this->db->query("select count(*) as colname from tblusers where role = '1'")->result_array();

我怀疑这会起作用。

Not positive, but it seems that all you need is to give that column a name. For example,

... = $this->db->query("select count(*) as colname from tblusers where role = '1'")->result_array();

I suspect that will work.

英雄似剑 2024-11-13 07:04:06

要读取 count(*) 索引,请使用以下命令创建一个可读字段:

select count(*) as cnt from tblusers where role = 1

现在您将拥有:

Array ( [0] => Array ( [cnt] => 2 ) )

For reading the count(*) index use the following to create a readable field:

select count(*) as cnt from tblusers where role = 1

Now you will have:

Array ( [0] => Array ( [cnt] => 2 ) )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文