Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在codeigniter中对计数查询执行num_rows()?_Php_Mysql_Codeigniter - Fatal编程技术网

Php 如何在codeigniter中对计数查询执行num_rows()?

Php 如何在codeigniter中对计数查询执行num_rows()?,php,mysql,codeigniter,Php,Mysql,Codeigniter,这项工作: $sql = "SELECT id FROM `users` WHERE `account_status` = '" . $i . "'"; $query = $this->db->query($sql); var_dump($query->num_rows()); 但这并不是: $sql = "SELECT COUNT(*)

这项工作:

        $sql = "SELECT id
                FROM `users`
                WHERE `account_status` = '" . $i . "'"; 
        $query = $this->db->query($sql);
        var_dump($query->num_rows());
但这并不是:

        $sql = "SELECT COUNT(*)
                FROM `users`
                WHERE `account_status` = '" . $i . "'"; 
        $query = $this->db->query($sql);
        var_dump($query->num_rows());
如何在COUNT*查询中执行num_行?第二种方法是否性能更好?

计算*只会得到一个包含行数的单数行,而不是结果本身

要访问COUNT*,您需要执行以下操作

$result = $query->row_array();
$count = $result['COUNT(*)'];

第二个选项执行得更好,因为它不需要将数据集返回到PHP,而只需要一个计数,因此更优化。

这将只返回一行,因为您只需要选择一个计数。在本例中,您将在$query上使用mysql\u num\u行

如果要获取每个ID的计数,请将GROUP BY ID添加到字符串的末尾


在性能方面,永远不要在查询中使用*。如果一个表中有100个唯一的字段,并且您想要全部获得它们,那么您将写出所有100个字段,而不是*。这是因为*每次抓取一个字段时,都必须重新计算它必须进入的字段数,这需要花费更多的时间来调用。

COUNT查询中的num\u行实际上总是1。它是一个聚合函数,没有GROUPBY子句,因此所有行都被分组到一个集合中。如果您想要计数的值,您应该给它一个标识符SELECT count*as myCount…,然后首先使用访问结果的常规方法,仅返回结果并获取其“myCount”属性。

我建议不要使用相同的参数执行另一个查询,而是立即在CI中运行SELECT FOUND_ROWS

,实际上非常简单,您只需

$this->db->where('account_status', $i);
$num_rows = $this->db->count_all_results('users');
var_dump($num_rows); // prints the number of rows in table users with account status $i
查询返回的行数。注意:在本例中,$query是分配给查询结果对象的变量:

$query = $this->db->query('SELECT * FROM my_table');

echo $query->num_rows();
根据这一点,我们可以使用以下方法:

$this->db->where('account_status', $i); // OTHER CONDITIONS IF ANY
$this->db->from('account_status'); //TABLE NAME
echo $this->db->count_all_results();
如果我们想在没有任何条件的情况下获得表中的总行数,只需使用

echo $this->db->count_all_results('table_name'); // returns total_rows presented in the table

+1,但值得一提的是,您可以为该列设置别名,然后使用它。像选择计数*作为cnt。。。然后使用$result['cnt'].@Corbin yea我必须这样做,COUNT*键由于某种原因不起作用。跳过一步,直接引用结果,而不首先将其设置为数组:COUNT*as cnt。。。然后通过$query->row0->cnt@Frug这对我不起作用,但是使用“c”别名和$query->row->c didI还应该提到使用CodeIgniter活动记录类来帮助更好地组织代码。@danneth在7年前就已经提出了这个建议,这表明不需要使用from调用。请阅读CI文档-你的观点是什么?医生说你的第二个代码片段应该调用count_all,而不是count_all_results,但这不是OP所要求的。这与发布的问题一点也不相似。我认为这篇只写代码的帖子不是答案,也无法出售。OP专门寻找一种codeigniter技术,而不是一般的mysql_uuu建议。OP不是连续两次查询,而是提供两种技术进行比较,作为努力的证明。
$this->db->where('account_status', $i); // OTHER CONDITIONS IF ANY
$this->db->from('account_status'); //TABLE NAME
echo $this->db->count_all_results();
echo $this->db->count_all_results('table_name'); // returns total_rows presented in the table