note 10029 deleted from mysql_num_rows by danbeck
| From: | danbeck@php.net | Date: | Thu, 08 Mar 2001 21:25:02 +0000 |
| Subject: | note 10029 deleted from mysql_num_rows by danbeck | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-12069@lists.php.net to get a copy of this message | ||
If you don't need the data and simply need row counts, don't use
mysql_num_rows as mysql can count much faster with its own count function.
In such cases, consider the following :
function getRowCount ($db_table,$db_column='',$db_value='')
{
$q = "select count(*) from $db_table";
if (!empty($db_column)) {
$q .= " WHERE $db_column='$db_value'";
}
$r = mysql_query ("$q") or die (mysql_error());
Return mysql_result($r,0);
}
Uses :
------
// Returns total number of rows in table
getRowCount('tablename');
// Returns number of NULL (empty) rows in a table column
getRowCount('tablename','columnname');
// Returns number of VALUES ($db_value) in table column
getRowCount('tablename','columnname','value');
Note :
-------
mysql_num_rows() makes more sense if you've already done the SELECT and
need to know how many rows are in your result set.