PHP PDO – Num Rows

According to the manual, there is a PDOStatement->rowCount method ; but it shouldn’t be used (quoting) :

For most databases,
PDOStatement::rowCount() does not
return the number of rows affected by
a SELECT statement.
Instead, use
PDO::query() to issue a SELECT COUNT(*) statement with the same
predicates as your intended SELECT
statement, then use
PDOStatement::fetchColumn() to
retrieve the number of rows that will
be returned.
Your application can then
perform the correct action.

If you already have a recordset, and want to know how many lines are in it, you’ll have to fetch the data, using one of the fetch* methods ; and use count — like you suggested.

Leave a Comment