Welcome Guest, Not a member yet? Register   Sign In
how to access the count(*) property?
#11

[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.
#12

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

[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().
#14

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

[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.




Theme © iAndrew 2016 - Forum software by © MyBB