04-02-2009, 08:17 PM
|
#4 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by WebSavvy
I always use count Vs using num_rows. My site uses a lot of queries, and the load on the server is fairly light given the size of my site.
Using an asterisk (*) wildcards count() to consider all fields. It does add a very small (not even noticeable) lag in the query process. However, if you only need to get count totals from a specific field, you can use just that field_name and it serves the same purpose.
I guess it all comes down to personal preferences. Some programmers like to code it one way, and others another way.
|
Quote:
Originally Posted by Village Idiot
When left between the choice of leaving the SQL server to process returned data and letting the script handle it, it is almost always a better idea to let the database do it. Using PHP to count the rows runs an unnecessary query which returns more rows than needed (the initial one), an another completely different one (num_rows runs another query to my understanding). Using the count function keeps it to one small consise query.
|
Thanks guys/gals:
So just to confirm, yes I only need the count, so I don't need any other data from the table. The code below sounds like the BEST and most efficient way to retrieve count...
Notice I am using "user_id" instead of "*"......
Code:
$query = mysql_query("SELECT count(user_id) as user_count FROM users");
$results = mysql_fetch_assoc($query);
$results[user_count]";
I am also using mysql_fetch_assoc instead of mysql_fetch_array since I don't need to return any array data although I'm not sure if in this situation, it really makes much of a difference.
|
|
|
|