codeigniter count rows in view & controller

This article will show you how to count query results and number of rows in table in codeigniter. At times you may be interested in knowing the number of rows in a table rather than fetching the records itself also you learn Also Read: Count Query Codeigniter Tutorial. Or to know if your mysql DB query actually returns any records at all. While you can count it using select mysql DB query, there are better ways to fetching the job done in codeingniter.

codeigniter count rows

CodeIgniter Count Query Results & Number of Rows in Table

Active Records library provides some handy functions to count the mysql DB query resultset or get row count in a table. We’ll see what those functions are and how to use them here.

Sample MySQL Table:

CodeIgniter Count Query Results & Number of Rows in Table
CodeIgniter Count Query Results & Number of Rows in Table

Count Query Results in CodeIgniter:

There are two best ways to count mysql DB query results in php codeigniter. The first one is using the num_rows() function.

1. Function: num_rows()

This function counts the number of rows returned by a mysql DB query. You have to use it like this,

$this->db->where('student_regno >=', 5);
$query = $this->db->get('Students');
echo $query->num_rows();

Sometimes you may wonder if your database mysql DB query actually returns any results at all. In such case you can first check if the result set is empty or not.

if ($query->num_rows() > 0)
{
   foreach ($query->result() as $row)
   {
      echo $row->student_regno;
      echo $row->student_name;
      echo $row->category;
   }
}

2. Function: count_all_results()

The count_all_results() function is another way to determine the number of rows returned by a particular Active Record mysql DB query. This works with all sorts of conditional queries using where, like clauses.

$this->db->select('*');
$this->db->from('Students');
$this->db->like('category', 'Test');
echo $this->db->count_all_results();

If you easy want to fetching the row count of a table rather than of a mysql DB query, just pass the db table name as parameter to count_all_results() function.

echo $this->db->count_all_results('Students');

Also Read: Update query in codeigniter Example

3. CodeIgniter – Count Number of Rows in Table:

If you want to count just the number of rows in a database table you can use the function $this->db->count_all(). Just pass the database table name to the function to fetching the row count such as this,

echo $this->db->count_all('Students');


Related FAQ

Here are some more FAQ related to this Article:

  1.   Remove # from URL VueJS Example
  2.   How to Convert String to Object in Angularjs?
  3.   PHP Web Application create Installer scripts
  4.   JQuery Ajax Dynamic Treeview using PHP MySQL Example
  5.   Laravel Number of days between two inputted dates
  6.   INSERT Multiple Records in Laravel 5.8
  7.   how to run sql query in command prompt?
  8.   Update query in codeigniter Example
  9.   Dynamically Add-remove Form Fields in jquery with PHP and mysql
  10.   All coins and Cryptocurrency Data API PHP, Ruby, Python