Re: count vs mysql_num_rows

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

 



On 10/8/07, Kevin Murphy <php@xxxxxxxxxxxxxxxxxx> 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;

i would do a select count() from the database. no sense in returning
all the row data back to the script just to do a count on it there.
that's additional network/socket transfer between the app and the
database, then processing on the app side to collect all the rows into
memory, etc.

you also don't need to do select count(id) as count, just do a select
count(id) and do a mysql_fetch_array($results, MYSQL_NUM) or
mysql_fetch_row($results) - you don't need to make it an associative
array, since it's only one column and the name is irrelevant.

mysql_fetch_row() is the best function for what you want. you'll save
any additional PHP overhead with different types of arrays being
setup.

-- 
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