CodeIgniter Forums
how to access the count(*) property? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: how to access the count(*) property? (/thread-26744.html)

Pages: 1 2


how to access the count(*) property? - El Forum - 01-28-2010

[eluser]jmadsen[/eluser]
er...no....

If the result set is:


(Emp)Count name address telephone
===============================
6 Big Corp. 12 Mystreet 123-4567
4 Small Corp. 12 Yourstreet 345-6789


Then the number of rows is 2, the counts are values summed in the database

num_rows() is the rows returned in the resultset, not an aggregate function.


how to access the count(*) property? - El Forum - 01-29-2010

[eluser]Chad Fulton[/eluser]
Oh, you're quite right. Good call!


how to access the count(*) property? - El Forum - 01-29-2010

[eluser]Phil Sturgeon[/eluser]
[quote author="jmadsen" date="1264754428"]Uh, Phil...


the number of rows returned is not the same as count(*)


"SELECT count(*) `count` FROM table WHERE ..."

->num_rows() is 1
$row->count is how ever many rows meet the where clause...

Maybe you want to read the post? ;-)[/quote]

I was giving general counting examples to show corrected syntax as most of the posts so far were a little off.

And of course if you run num_rows() on a COUNT() query you will only get 1, why would you get any more? The only value returned is an integer counting how many results matched the WHERE criteria, which is exactly what num_rows() would do.

If you are only wanting to return a count, then COUNT() will be the same as num_rows(). If you need data back to then you need to use num_rows().


how to access the count(*) property? - El Forum - 01-29-2010

[eluser]sandeep nami[/eluser]
@ Phil Sturgeon
Thankyou for ur detailed explanation


how to access the count(*) property? - El Forum - 02-07-2010

[eluser]jmadsen[/eluser]
If you mix an aggregate (Count(), Sum(), Max(), etc. ) with non-aggregate fields (name, address, etc) such as this:

$row = $this->db->select('count(*), name, address, telephone')

Then you MUST use a GROUP BY.

If you are grouping, it's a good bet (not always) that you are prepared for more than one group. So num_rows() is not correct to use here.

---

In addition, the real problem here was that the poster did not know that he should alias calculated rows of any sort, and reference them.

That's what people should learn from this post.