64

How can I SELECT some data from one of my tables and then count how many rows are returned using CodeIgniter?

1

8 Answers 8

127

Have a look at the result functions here:

$this->db->from('yourtable');
[... more active record code ...]
$query = $this->db->get();
$rowcount = $query->num_rows();
Sign up to request clarification or add additional context in comments.

2 Comments

I was banging my head for hours, I couldn't recall the function's name... and it's not easy to find in official documentation either. I used print_r on object resulting from get_where method to guess the function's name! =)
38

AND, if you just want to get a count of all the rows in a table

$table_row_count = $this->db->count_all('table_name');

1 Comment

While this script is valid, count_all() will ignore any other (potentially vital) clauses like WHERE.
32

This goes to you model:

public function count_news_by_category($cat)
{
    return $this->db
        ->where('category', $cat)
        ->where('is_enabled', 1)
        ->count_all_results('news');
}

It'a an example from my current project.

According to benchmarking this query works faster than if you do the following:

$this->db->select('*')->from('news')->where(...); 
$q = $this->db->get(); 
return $q->num_rows();

1 Comment

I am skeptical of the now-broken benchmark link. Why would a query that only returns the count integer be slower than a query which returns all column values of the qualifying rows AND then counts the result rows?
16

If you only need the number of rows in a query and don't need the actual row data, use count_all_results

echo $this->db
       ->where('active',1)
       ->count_all_results('table_name');

Comments

3

You can do this in two different ways:

  1. $this->db->query(); //execute the query
     $query = $this->db->get() // get query result
     $count = $query->num_rows() //get current query record.

  2.  $this->db->query(); //execute the query
      $query = $this->db->get() // get query result
      $count = count($query->results()) 
          or   count($query->row_array())  //get current query record.

1 Comment

count($query->row_array()) is incorrect for this asked question -- that will count the number of returned columns in the first row of the result set. You probably meant count($query->result_array()).
2

This is also a very useful function if you are looking for a rows or data with where condition affected

function num_rows($table)
    {   
       return $this->db->affected_rows($table);
    }

1 Comment

This would only be appropriate after a WRITE query, right? This question is asking about counting the results of a READ query.
2
$this->db->select('count(id) as rows');
$this->db->from('table_name');
$this->db->where('active',1);
$query = $this->db->get();
foreach($query->result() as $r)
{
   return $r->rows;
}

3 Comments

This causes a SELECT query being issued including a COUNT() SQL function call. The accepted answer should be taken instead as it prevents this by calling $query->num_rows() instead. For example, num_rows() of mysqli driver returns the result of mysqli_num_rows() PHP function for last query id. Please note: Not all database drivers have a native way of getting the total number of rows for a result set. When this is the case, all of the data is prefetched and count() is manually called on the resulting array in order to achieve the same result. (from documentation).
Why bother creating a 2d array from the result set when you only need to access the first column value from the first row? I do not recommend result() or the loop in this code-only answer. This asked question clearly states that the count is needed in addition to the result set data.
0
$sql = "SELECT count(id) as value FROM your_table WHERE your_field = ?";
$your_count = $this->db->query($sql, array($your_field))->row(0)->value;
echo $your_count;

1 Comment

More simply ->row('value') How to return only a specified column value from the first row of a CodeIgniter query. This asked question, however, clearly states that it wants to return result set data AND count the number of rows. This code-only answer is inappropriate for this page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.