note 10029 modified in mysql_num_rows by jmoore

From: Date: Wed, 20 Dec 2000 18:57:34 +0000
Subject: note 10029 modified in mysql_num_rows by jmoore
Groups: php.notes 
Request: Send a blank email to php-notes+get-8975@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. http://www.php.net/manual/function.mysql-num-rows.php

« previous php.notes (#8975) next »