user3557031
user3557031

Reputation:

PDO rowCount() Supported Databases

I'm just looking for a list of supported databases with the rowCount() method from PDO. I know this sounds like a RFTM question, but I honestly cannot find anything at all relating to which specific databases it will work on. All I know about it is this from the manual:

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

I also know this works in MySQL, and will not work on MS Server but what about the other databases which PDO supports?

Upvotes: 1

Views: 163

Answers (1)

James Cobban
James Cobban

Reputation: 421

Because of this documented restriction, and recognizing that all current computers have lots of memory, the most portable, and in most cases fastest, implementation is to issue $result = $stmt->fetchAll() and then use count($result).

In my opinion the code also looks cleaner since the loop through the returned values becomes a simple foreach($result as $row). This is a faster implementation for two reasons:

  • The database driver can optimize the fetchAll, for example MySQL is documented to buffer the result in this case
  • The loop through the records does not involve a call out of your script to the driver.

Since you are almost certainly going to be reading every row of the reply before completing the response to the end user, why not do it all at once with a single call to the driver? Implementing rowCount for SELECT in a DB independent way requires issuing a separate SELECT COUNT(*) request.

It takes almost as much effort for the driver to perform the SELECT COUNT(*) as the actual SELECT, so you would be doubling the time to process the request.

Upvotes: 1

Related Questions