PDO apparently has no means to count the number of rows returned from a select query (mysqli
has the num_rows
variable).
Is there a way to do this, short of using count($results->fetchAll())
?
PDO apparently has no means to count the number of rows returned from a select query (mysqli
has the num_rows
variable).
Is there a way to do this, short of using count($results->fetchAll())
?
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 aSELECT
statement.
Instead, usePDO::query()
to issue aSELECT COUNT(*)
statement with the same predicates as your intendedSELECT
statement, then usePDOStatement::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.