CodeIgniter’s Query Builder class has support to get several results without getting all results.
Use PHP count() method
This is commonly used if we want to get both results’ data and the number of data sets in the results.
$this->db->select('*'); $this->db->from('posts'); $this->db->where('user_id', '10'); $query = $this->db->get(); $results = $query->result_array(); echo count($results);
Use num_rows() method
$this->db->select('count(*)'); $this->db->from('comments'); $this->db->where('user_id','2'); $query = $this->db->get(); echo $query->num_rows();
Use count_all_results() method
$this->db->select('*'); $this->db->from('posts'); $this->db->where('title LIKE', '%CodeIgniter%'); $total = $this->db->count_all_results();