Occasionally you bump into a situation where you have to count the no of rows in a table in database.
There is one quick n dirty method to do that..
$noofrows = mysql_num_rows(mysql_query("select * from table"));
This method is inefficient and wont show errors while executing.
Alternatively you can use a function which returns the no of rows in the table.. it takes table name and db connection handle as input and returns the row count or number of rows present in that table. Most of the time i use this function in pagination scripts.
function getnoofrows($table,$con){ if($result = mysql_query("select count(*) as rowcount from table",$con)){ $row = mysql_fetch_array($result); return $row['rowcount']; } else { //show MySQL error die(mysql_error()); } }
usage:
$noofrows = getnoofrows($table,$con);