Re: count vs mysql_num_rows

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Kevin Murphy wrote:
I've got a little function that just checks to see if something is in a mysql db. There are several ways to do this and was curious as to the best way. The following are 2 (simplified) versions that work just fine. If these are the best ways, which of the following is better from a performance standpoint. And if there is a way that is better than one of these, I'd love to know what that is.

$query = "SELECT count(id) as count FROM wncci_intranet.iAdmin_users WHERE name = '$name' LIMIT 1";
$results = mysql_query($query);
$row = mysql_fetch_array($results);
$count = $row["count"];
return $count;

OR

$query = "SELECT id FROM wncci_intranet.iAdmin_users WHERE name = '$name' LIMIT 1";
$results = mysql_query($query);
$count = mysql_num_rows($results);
return $count;


Thanks.


Is this a joke?  You are using a LIMIT 1, so your count is always going to be 1.

But, if you didn't use the LIMIT 1 and really wanted to see how many the results would be found, then I would do it like this.

$SQL = "SELECT	count(id)
	FROM	iAdmin_users
	WHERE	name = '{$name}'";
$results = mysql_query($SQL) OR die('Mysql Error: '.mysql_error());

$count = 0;
if ( $results ) {
	list($count) = mysql_fetch_row($results);
}
return $count;

--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
    by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux