1

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.

3
  • try sizeof($query->result()) Commented Jul 22, 2014 at 11:43
  • Try count($query->result()) Commented Jul 22, 2014 at 11:45
  • 2
    $query->num_rows() as stated in the documentation Commented Jul 22, 2014 at 11:46

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try count_all_results

Example:

echo $this->db->count_all_results('my_table');
// Produces an integer, like 25

$this->db->like('title', 'match');
$this->db->from('my_table');
echo $this->db->count_all_results();
// Produces an integer, like 17 

OR

echo $this->db->where('group_id',1)->count_all_results('users');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.