5

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?

1
  • I should also mention to use the CodeIgniter Active Record class to help organize your code a bit better. Commented Jul 6, 2012 at 1:48

9 Answers 9

14

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.

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

4 Comments

+1, but it might be worth mentioning that you can alias the column and then use that. Like SELECT COUNT(*) AS cnt ... and then use $result['cnt'].
@Corbin yea I had to do it like that, COUNT(*) as the key wasn't working for some reason.
Skip a step and refer directly to the result without setting it as an array first: COUNT(*) AS cnt ... and then access it via $query->row(0)->cnt
@Frug that didn't work for me, but using 'c' alias and $query->row->c did
11

In CI it's really simple actually, all you need is

$this->db->where('account_status', $i);
$num_rows = $this->db->count_all_results('users');
var_dump($num_rows); // prints the number of rows in table users with account status $i

Comments

8
$query->num_rows()

The number of rows returned by the query. Note: In this example, $query is the variable that the query result object is assigned to:

$query = $this->db->query('SELECT * FROM my_table');

echo $query->num_rows();

Comments

4

num_rows on your COUNT() query will literally ALWAYS be 1. It is an aggregate function without a GROUP BY clause, so all rows are grouped together into one. If you want the value of the count, you should give it an identifier SELECT COUNT(*) as myCount ..., then use your normal method of accessing a result (the first, only result) and get it's 'myCount' property.

Comments

3

As per CI Docs we can use the following,

$this->db->where('account_status', $i); // OTHER CONDITIONS IF ANY
$this->db->from('account_status'); //TABLE NAME
echo $this->db->count_all_results();

If we want to get total rows in the table without any condition, simple use

echo $this->db->count_all_results('table_name'); // returns total_rows presented in the table

3 Comments

@danneth already offered this advice ~7 years earlier and it shows that the from() call is not necessary.
Kindly read the CI Docs - codeigniter.com/userguide3/database/…
And your point is? The docs say yoir second snippet should be calling count_all instead of count_all_results() -- but that is not what the OP is asking for.
0

it's my way of solving the above given question

model

$this->db->select('count(id) as ids');
$this->db->where('id', $id);
$this->db->from('your_table_name');

thanks

Comments

-1

This will only return 1 row, because you're just selecting a COUNT(). you will use mysql_num_rows() on the $query in this case.

If you want to get a count of each of the ID's, add GROUP BY id to the end of the string.

Performance-wise, don't ever ever ever use * in your queries. If there is 100 unique fields in a table and you want to get them all, you write out all 100, not *. This is because * has to recalculate how many fields it has to go, every single time it grabs a field, which takes a lot more time to call.

1 Comment

The OP is specifically looking for a codeigniter technique, not general mysql_ advice.
-2

I'd suggest instead of doing another query with the same parameters just immediately running a SELECT FOUND_ROWS()

1 Comment

The OP isn't making two consecutive queries, the OP is offering two techniques to compare (as proof of effort).
-2
    $list_data = $this->Estimate_items_model->get_details(array("estimate_id" => $id))->result();
    $result = array();
    $counter = 0;
    $templateProcessor->cloneRow('Title', count($list_data));
    foreach($list_data as $row) {
        $counter++;
        $templateProcessor->setValue('Title#'.$counter, $row->title);
        $templateProcessor->setValue('Description#'.$counter, $row->description);
        $type = $row->unit_type ? $row->unit_type : "";
        $templateProcessor->setValue('Quantity#'.$counter, to_decimal_format($row->quantity) . " " . $type);
        $templateProcessor->setValue('Rate#'.$counter, to_currency($row->rate, $row->currency_symbol));
        $templateProcessor->setValue('Total#'.$counter, to_currency($row->total, $row->currency_symbol));   
    }

1 Comment

This doesn't resemble the posted question in the slightest. I reckon this code-only post is Not An Answer (and unsalvageable).

Your Answer

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