首页 > 解决方案 > 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)?

有没有更有效的方法来实现我想要的?

标签: phpcodeignitercodeigniter-3

解决方案


你只能做:

public function selectRecords() {
    $this->db->select('*');
    $this->db->from('records');
    $query = $this->db->get();
    return $query->result_array();
}

$records = $this->selectRecords();
$count = count($records);

推荐阅读