php 如何在 codeigniter 中对 COUNT 查询执行 num_rows()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11354802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:19:47  来源:igfitidea点击:

How to do a num_rows() on COUNT query in codeigniter?

phpmysqlcodeigniter

提问by TK123

This works:

这有效:

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

But this doesn't:

但这不会:

        $sql = "SELECT COUNT(*)
                FROM `users`
                WHERE `account_status` = '" . $i . "'"; 
        $query = $this->db->query($sql);
        var_dump($query->num_rows());

How to do a num_rows on a COUNT(*) query? Also is doing it the 2nd way any better performance wise?

如何对 COUNT(*) 查询执行 num_rows?也是以第二种方式做更好的性能吗?

回答by Steven Lu

Doing a COUNT(*)will only give you a singular row containing the number of rows and not the results themselves.

执行 aCOUNT(*)只会给你一个包含行数而不是结果本身的单行。

To access COUNT(*)you would need to do

要访问COUNT(*)你需要做的

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

The second option performs much better since it does not need to return a dataset to PHP but instead just a count and therefore is much more optimized.

第二个选项的性能要好得多,因为它不需要将数据集返回给 PHP,而只是一个计数,因此更加优化。

回答by danneth

In CI it's really simple actually, all you need is

在 CI 中,它实际上非常简单,您只需要

$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

回答by Varun Naharia

$query->num_rows()

The number of rows returned by the query. Note: In this example, $query is the variable that the query result object is assigned to:

查询返回的行数。注意:在本例中,$query 是查询结果对象分配给的变量:

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

echo $query->num_rows();

回答by Chris Trahey

num_rows on your COUNT() query will literally ALWAYS be 1. It is an aggregate function without a GROUP BY clause, so all rows are grouped together into one. If you want the valueof the count, you should give it an identifier SELECT COUNT(*) as myCount ..., then use your normal method of accessing a result (the first, only result) and get it's 'myCount' property.

COUNT() 查询中的 num_rows 字面上总是 1。它是一个没有 GROUP BY 子句的聚合函数,因此所有行都组合在一起。如果你想要计数的,你应该给它一个 identifier SELECT COUNT(*) as myCount ...,然后使用你的正常方法访问结果(第一个,唯一的结果)并获取它的 'myCount' 属性。

回答by Silambarasan R.D

As per CI Docswe can use the following,

根据CI Docs,我们可以使用以下内容,

$this->db->where('account_status', $i); // OTHER CONDITIONS IF ANY
$this->db->from('account_status'); //TABLE NAME
echo $this->db->count_all_results();

If we want to get total rows in the table without any condition, simple use

如果我们想在没有任何条件的情况下获取表中的总行数,简单使用

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

回答by Alper Alver

    $list_data = $this->Estimate_items_model->get_details(array("estimate_id" => $id))->result();
    $result = array();
    $counter = 0;
    $templateProcessor->cloneRow('Title', count($list_data));
    foreach($list_data as $row) {
        $counter++;
        $templateProcessor->setValue('Title#'.$counter, $row->title);
        $templateProcessor->setValue('Description#'.$counter, $row->description);
        $type = $row->unit_type ? $row->unit_type : "";
        $templateProcessor->setValue('Quantity#'.$counter, to_decimal_format($row->quantity) . " " . $type);
        $templateProcessor->setValue('Rate#'.$counter, to_currency($row->rate, $row->currency_symbol));
        $templateProcessor->setValue('Total#'.$counter, to_currency($row->total, $row->currency_symbol));   
    }

回答by badcircle

I'd suggest instead of doing another query with the same parameters just immediately running a SELECT FOUND_ROWS()

我建议不要使用相同的参数进行另一个查询,而是立即运行 SELECT FOUND_ROWS()

回答by jay

This will only return 1 row, because you're just selecting a COUNT(). you will use mysql_num_rows()on the $queryin this case.

这只会返回 1 行,因为您只是选择了一个COUNT(). 在这种情况下,您将使用mysql_num_rows()on $query

If you want to get a count of each of the ID's, add GROUP BY idto the end of the string.

如果您想获得每个ID's的计数,请添加GROUP BY id到字符串的末尾。

Performance-wise, don't ever ever ever use *in your queries. If there is 100 unique fields in a table and you want to get them all, you write out all 100, not *. This is because *has to recalculate how many fields it has to go, every single time it grabs a field, which takes a lot more time to call.

性能方面,永远不要*在您的查询中使用。如果表中有 100 个唯一字段,而您想获取所有字段,则写出全部 100 个,而不是*. 这是因为*必须重新计算它必须去多少个字段,每次它抓取一个字段时,这需要更多的时间来调用。