Re: count vs mysql_num_rows
| From: | Jim Lucas | Date: | Mon, 08 Oct 2007 19:23:31 +0000 |
| Subject: | Re: count vs mysql_num_rows | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-262931@lists.php.net to get a copy of this message | ||
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