How to quickly and correctly calculate the number of records in the mysql database using PDO?
So

$query=$dbh->query("SELECT id FROM users"); $members=$query->rowCount(); 

or so

 $query=$dbh->query("SELECT COUNT(*) as count FROM users"); $query->setFetchMode(PDO::FETCH_ASSOC); $row=$query->fetch(); $members=$row['count']; 

Thank.

    2 answers 2

    Logically the second. In the first case, all the lines from the database are unloaded (especially fun, if there are 1000+), the second ... Created to count the number of records :)

    • 2
      Appearance is deceptive ... =) four lines will be completed faster than two) - sinedsem

    Of course, only the second option. Moreover, it can be written much easier:

     $members=$dbh->query("SELECT COUNT(*) as count FROM users")->fetchColumn();