Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

getRowTotal for mysql

Code 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.
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions