Full-service Internet Marketing & Web Development
Recent Posts

Sponsors
![]() |
getRowTotal for mysqlCode Wizard, 12-30-2006 |
If you don't need the data and simply need row counts (number of rows), don't use mysql_num_rows() as mysql can count much faster with its own count function. In such cases, use the following little function.
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.
<?php
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);
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');
You can assign as a variable '$count = getRowTotal(...)' or
print it 'print getRowTotal(...)' -- read above description
for more information on when and why you'd want to use this
function.
|
|
Subscribe Now to receive new posts via Email as soon as they come out.
Comments
Post your comments

