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())
?
PDOStatement::rowCount() returns the number of rows affected by a DELETE, INSERT, or UPDATE statement. print("Return number of rows that were deleted:\n"); $count = $del->rowCount();
We can get the total number of rows in a table by using the MySQL mysqli_num_rows() function. Syntax: mysqli_num_rows( result ); The result is to specify the result set identifier returned by mysqli_query() function.
To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With