phpmysqlcodeigniteractiverecordcount

Count number of query results in a CodeIgniter application


I have my query:

$query = $this->db
    ->get_where('users', array('group_id' => 7, 'active' => 1));

It works if I use:

print_r ($query->result());

All I want to do, is get the total number of rows that match my query. I've tried num_rows and count(), but I can't get either of them to work!

At the moment, I am just testing this in my view, but will move it over to my model when I figure it out.


Solution

  • You need to use $query->num_rows(). It will return total number of rows returned using your query.

    for example :

    $query = $this->db->query("YOUR QUERY");
    
        if ($query->num_rows() > 0)
        {
           //DO your stuff
        } 
    

    For more reference see Documentation.