CodeIgniter& C中的$ query> num_rows()和$ this-> db-> count_all_results()建议一个 [英] difference between $query>num_rows() and $this->db->count_all_results() in CodeIgniter & which one is recommended
问题描述
在一个场景中,我需要知道查询将返回的记录集的计数,在codeigniter中可以通过 $ query-> num_rows()
或 $ this-> db-> count_all_results()
。 code>你首先执行查询,然后你可以检查你有多少行。 count_all_results()
另一方面只给出查询会产生的行数,但不会给出实际的结果集。
// num rows example
$ this-> db-> select('*');
$ this-> db-> where('whatever');
$ query = $ this-> db-> get('table');
$ num = $ query-> num_rows();
//这里你可以用$ query
做一些事情// count all example
$ this-> db-> where('whatever');
$ num = $ this-> db-> count_all_results('table');
//这里你只有$ num,没有$ query
In a scenario I need to know the count of recordset a query will return, which in codeigniter can be done by $query->num_rows()
or $this->db->count_all_results()
. Which one is better and what is the difference between these two?
With num_rows()
you first perform the query, and then you can check how many rows you got. count_all_results()
on the other hand only gives you the number of rows your query would produce, but doesn't give you the actual resultset.
// num rows example
$this->db->select('*');
$this->db->where('whatever');
$query = $this->db->get('table');
$num = $query->num_rows();
// here you can do something with $query
// count all example
$this->db->where('whatever');
$num = $this->db->count_all_results('table');
// here you only have $num, no $query
这篇关于CodeIgniter& C中的$ query> num_rows()和$ this-> db-> count_all_results()建议一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!