PDO equivalent of mysql_num_rows()?

I am trying to figure out how I can get the number of rows returned by a fetch. Is there an equivalent of mysql_num_rows() that comes with PDO? I noticed PDOStatement->rowCount(), however; on select statements this is not a guaranteed way of getting back the number of rows returned by the statement as this behavior is not guaranteed for all databases.

Anyone have any ideas on how to go about getting the number of rows?

You can pretty much consider PDOStatement->rowCount() the PDO equivalent of mysql_num_rows() because both are guaranteed to work with MySQL. However, if what you are looking for is a database independent API, you cannot rely on PDOStatement->rowCount() and certainly not on mysql_num_rows() for obvious reasons.

Using SELECT COUNT(*) statement will serve your purpose.

AFAIK, you have to count the rows in the array.


echo count( $stmt->fetchAll( PDO::FETCH_BOTH ) );

Maybe there is a better way?