note 10029 added to mysql_num_rows
| From: | philip at thepr dot chek dot com | Date: | Fri, 08 Dec 2000 06:41:57 +0000 |
| Subject: | note 10029 added to mysql_num_rows | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-8516@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, I use the following little
function, perhaps it'll be of some use :
function getRowTotal ($db_table,$db_count_field='',$db_count_value='')
{
$q = "select count(*) from $db_table";
if (!empty($db_count_field)) {
$q .= " WHERE $db_count_field='$db_count_value'";
}
$r = mysql_query ("$q") or die (mysql_error());
Return mysql_result($r,0);
}
Uses :
------
// Returns number of rows in table
getRowTotal('tablename');
// Returns number of NULL (empty) rows in a table column
getRowTotal('tablename','columnname');
// Returns number of VALUES in table column
getRowTotal('tablename','columnname','value');
You can assign as a variable '$count = getRowTotal(...)' or print it 'print
getRowTotal(...)'
Notes :
-------
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.
http://www.php.net/manual/function.mysql-num-rows.php