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

Read Also:  PHP Inheritance - Multilevel and Multiple Inheritance in PHP

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.

Read Also:  pagination code in php with next and previous demo

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.

Read Also:  How to Integrate Google No CAPTCHA reCAPTCHA using VueJS

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. Read Also:  laravel 6 Delete Multiple Records Eloquent
  2. Read Also:  PHP Mailgun Sending Emails Tutorials
  3. Read Also:  Simple Vue Input Array Models Example
  4. Read Also:  $digest() Function using Angular Example
  5. Read Also:  Update query in codeigniter Example
  6. Read Also:  Get server memory and CPU usage in PHP Script (RAM & CPU Usage)
  7. Read Also:  How to send mail from localhost XAMPP using PHP?
  8. Read Also:  Redirect page after few seconds in PHP
  9. Read Also:  Redirect page after few seconds in PHP
  10. Read Also:  PHP Mailgun Sending Emails Tutorials