There are two ways to retrieve the number of rows selected from a database; either by using the PHP function mysql_num_rows() or by using the SQL COUNT() Function. Take a look at the following :
- Using PHP
$query = "SELECT (column) FROM table WHERE column = value";
$results = mysql_query($query);
$rows = mysql_num_rows($results);
echo $rows ;
- Using SQL
$query = "SELECT COUNT(column) FROM table WHERE column value";
$results = mysql_query($query);
$rows = mysql_fetch_array($results);
echo $rows[0];
Both will get the same results, experiments and discussions revealed that using SQL COUNT() function is much faster as it doesn't use much resources as the first method. Also using COUNT(*) is faster than using COUNT(column) because when you specify a certain field; MySQL has to open each row and look around for that field.
|