Page 1 of 1

PDO, fetched rows and the fastest way...

Posted: Wed Apr 05, 2006 9:34 am
by asgerhallas
I have a SELECT query that get one or more rows from a large table. The script must do different things if the result is either 0, 1 or 2 and more rows. I'm using PDO and is has no function like mysql_num_rows() ... what must I do then?

I thought about running a second query with COUNT(*), but is that efficient? Or is it better to do a count(fetchAll())?

Does anybody know the best way to do it?

/Asger

Posted: Wed Apr 05, 2006 9:43 am
by feyd
If you're going to always work on all the records returned, storing the results in an array and counting them like your second idea is fine, otherwise using a COUNT() may be better.

Posted: Wed Apr 05, 2006 9:49 am
by asgerhallas
Ok, thanks!

Does that COUNT(*) need to be in another query, or is there a smart way to have it as a row in the same query?
I've tried this:

Code: Select all

SELECT *, COUNT(*) cnt FROM table WHERE name='somename'
But it wants the GROUP BY in it - and I can't find out what to GROUP BY - any idea?

Posted: Wed Apr 05, 2006 10:03 am
by feyd
That query likely won't work the way you'd like it to. Best case, if you insist on getting the count in the same query is using a UNION.

Posted: Wed Apr 05, 2006 10:13 am
by asgerhallas
Ok... thank you very much!

/Asger