As we have already discussed in Chapter 2, the PDOStatement::rowCount()
method does not return the correct number of rows in a query. (It returns zero for both MySQL and SQLite.) The reason for such behavior is that the database management systems do not actually know this number until the last row of the query has been returned. The reason for the mysql_num_rows()
function (and similar functions for other databases) returns the row count is that it preloads the whole result set into memory when you issue the query.
While it may seem convenient, this behavior is not recommended. If the query returns 20 rows, then the script can afford the memory usage. But what if the query returns several hundred thousands rows? They will all be kept in memory so that, on high traffic sites, the server may run out of resources.
The only logical measure (and the only option available with PDO) is to instruct the database to count the number of rows itself. No...