counting number of rows in database. which method?
Posted: Fri Aug 17, 2007 7:00 pm
For example, I have a users table, and I want to find if a particular userID is of a valid user.
Two methods that I know of...
So which is recommended I use? (And in other similar situations.) This question has been annoying me since I started with PHP. Is it best to count with PHP or count with SQL ?
Thank you
Two methods that I know of...
Code: Select all
$result = mysql_query("SELECT userID FROM users WHERE userID = '$userID'");
$rows = mysql_num_rows($result);
if ( $rows == 1 ) { return true; } else { return false; }Code: Select all
$result = mysql_query("SELECT COUNT(userID) AS rows FROM users WHERE userID = '$userID'");
$row = mysql_fetch_assoc($result);
if ( $row['rows'] == 1 ) { return true; } else { return false; }Thank you