devxtech — 2009-01-29T12:01:52-05:00 — #1
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?
kailash_badu — 2009-01-29T12:32:41-05:00 — #2
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.
cups — 2009-01-29T12:33:47-05:00 — #3
AFAIK, you have to count the rows in the array.
echo count( $stmt->fetchAll( PDO::FETCH_BOTH ) );
Maybe there is a better way?
bhanson — 2009-01-29T12:35:33-05:00 — #4