Originally posted by Sonic_Rage
Ok, I know this might sound stupid, but what is the benefit of me using this comapared to this
Basically it's a matter of using the right tool for the job (in this case, MySQL instead of PHP).
In the second piece of code, all the data in the entire table is taken out of MySQL, piped to PHP and converted into a recordset (taking up who knows how much memory), and then the number of rows in that recordset is counted.
In the first piece, MySQL sends a single row, containing one field containing one number - a number which it is already keeping track of for its own purposes anyway. So for MySQL, the number of rows in the table is immediately to hand - and if MySQL already knows the number, it seems a bit redundant to have PHP calculate it all over again. But the big savings are in PHP's memory and loading on the communication between MySQL and PHP.
In short, getting MySQL to do the counting entails far less work is involved for both MySQL, PHP, and less has to be communicated between them. So everything runs faster and uses fewer resources and everyone's happy 🙂