php - CodeIgniter 中 $query>num_rows() 和 $this->db->count_all_results() 之间的区别 & 推荐哪一个

标签 php codeigniter

在某些情况下,我需要知道查询将返回的记录集的数量,这在 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/

相关文章:

php - 无需第三方服务即可同步移动应用程序和网络应用程序

mysql - 如何在我的 CodeIgniter 模型中使用 ON DUPLICATE KEY UPDATE?

php - 成员函数静态变量的作用域

php - 如何在 Yii 的索引页面上对用户进行身份验证

php - 如何将mysql数组转换成php数组

php - 多项选择 PHP

php - Codeigniter 表单验证错误消息

php - 使用 Codeigniter Escape 函数

php - 使用 PHP 按键和值取消设置数组

php - 通过引用传递是否避免创建新变量?