How to do a num_rows() on COUNT query in codeigniter?

codeigniter, mysql, php

Solution

Doing a `COUNT(*)` will only give you a singular row containing the number of rows and not the results themselves.

To access `COUNT(*)` you would need to do

$result = $query->row_array();
$count = $result['COUNT(*)'];

The second option performs much better since it does not need to return a dataset to PHP but instead just a count and therefore is much more optimized.

Problem

This works: ``` $sql = "SELECT id FROM `users` WHERE `account_status` = '" . $i . "'"; $query = $this->db->query($sql); var_dump($query->num_rows()); ``` But this doesn't: ``` $sql = "SELECT COUNT(*) FROM `users` WHERE `account_status` = '" . $i . "'"; $query = $this->db->query($sql); var_dump($query->num_rows()); ``` How to do a num_rows on a COUNT(*) query? Also is doing it the 2nd way any better performance wise?

Original source