Re: PDO user question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


Brandon: RAM is cheap, but not always expendable depending on what you're doing. Using count() on results from PDOStatement::fetchAll() is fine on small result sets. Keep in mind that this will certainly blow up on very large result sets, especially depending on your memory_limit for php. Avoiding this problem is the primary reason I use prepared statements for retrieving data.

If you're working with small result sets you could avoid using prepared statements altogether, which would greatly simplify doing something like getting a row count.

If you don't absolutely need your code to be portable across multiple database servers, I would encourage you to experiment with the rowCount() method and see if it works for your setup. (I ran a quick test in my mysql environment and it worked fine for a SELECT statement. Sqlite doesn't seem to support it because the actual sqlite library doesn't support it. Haven't looked into postgres or others.)

Another thing to be aware of (at least with mysql), and speaking to Lester's point, is that you can use SQL_CALC_FOUND_ROWS (with mysql, anyway) to get a total count when you are using LIMITs on your result set (which would be advisable if you're returning these results in a web interface).


On 09/09/2012 02:40 PM, brandon wrote:
That's how I'd do it. Extend the PDO interface on your abstract class to include a "num_rows()" method that utilizes that higher level count($this->result). It might be a little more overhead... but RAM is cheap... and there's always forking/extending the library in C/C++...


On 2012-09-09 11:49, Michael Stowe wrote:
How are you using the number?  Probably the easiest way is to utilize
PDOStatement::FetchAll() and then do a count() on that result set.

- Mike

Sent from my iPhone

On Sep 9, 2012, at 11:42 AM, Stefan Wixfort <stefan.wixfort@xxxxxx> wrote:

Hi Jim

I've had some success with querying using pdo and prepared statements as
well.  One thing that I'm curious about is

How does one handle the need to get the number of rows returned by a
Select?  The documentation is very clear that PDO doesn't return that
value for a Select statement (depending upon driver?) and there were a
couple of solutions that made no sense to me.  There was even one that
did a completely separate query just to get the row count which makes
even less sense.

I believe you are referring to "SELECT COUNT(*)..."
Because I couldn't find a different way I use that.

PHP Database Mailing List (
To unsubscribe, visit:

PHP Database Mailing List (
To unsubscribe, visit:

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux