在某些情况下,我需要知道查询将返回的记录集的数量,这在 codeigniter 中可以通过 $query->num_rows()
或 $this->db- 完成>count_all_results()
。哪个更好,这两个有什么区别?
最佳答案
使用 num_rows()
您首先执行查询,然后您可以检查您得到了多少行。另一方面,count_all_results()
只为您提供查询将产生的行数,但不会为您提供实际的结果集。
// 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
关于php - CodeIgniter 中 $query>num_rows() 和 $this->db->count_all_results() 之间的区别 & 推荐哪一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7036950/