CodeIgniter’s Query Builder class has support to get only number of results without getting all results.
Table of Contents
Use PHP count() method
This is commonly used if we want to get both results’ data and its number of data set in 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();