php - Codeigniter 选择和统计 MySQL 记录
问题描述
使用Codeigniter 3
,我想显示MySQL
数据库中表中的所有记录。我还想包括选择的记录数。
例如;
Showing x number of records;
record 1
record 2
record 3
etc
目前我有以下(有效);
// select all records
public function selectRecords() {
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
return $query->result_array();
}
// count all records
public function countRecords() {
$this->db->select('count(*) as count');
$this->db->from('records');
$query = $this->db->get();
return $query->row();
}
我的问题是我是否需要两个单独的查询才能实现这个(select and count
)?
有没有更有效的方法来实现我想要的?
解决方案
你只能做:
public function selectRecords() {
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
return $query->result_array();
}
和
$records = $this->selectRecords();
$count = count($records);
推荐阅读
- java - 使用 Java 8 可选 - 转换逻辑
- python - 在 Keras v2 中计算精度、召回率和 F1,我做对了吗?
- c - C定义/全局变量,它依赖于其他东西
- ios - 如何在按钮中使用两行显示来自两个 NSString 变量的两行字符串值
- highcharts - 如何在 highchart 的标签中使用 line-height 属性
- java - 按钮最有效的点击是什么?
- r - macOS 上 R 3.5 中的 devEMF 包无法构建
- symmetricds - SymmetricDS:路由器到所有或特定节点
- typescript - 打字稿。过滤功能属性和“不能调用类型缺少调用签名的表达式。”
- python - 如何以所需的方式转换字符串?