tags:

views:

64

answers:

4
$userid = $_SESSION[userid]
$sqll = mysql_query("SELECT count(id) FROM entries WHERE userid='$userid'");

Is using count(id) efficient? Or is there a better way?

+3  A: 

Make sure you have an index on your userid column (if it's a primary key, you already do).

If you've got userid as a primary key, then this should be an efficient query and will have the same efficiency as using count(*).

mopoke
+4  A: 

count(*) should do.

Make sure you escape $userid, though.

Edit: just half-a-word of explanation. count(column) counts all rows for which column is not null. Using * is supposed to be the most efficient way of counting rows regardless of content and more descriptive in case someone (including yourself) is ever going to (re)read the code.

Michael Krelin - hacker
A: 

Since you don't need to count the id column specifically you can use COUNT(*), which may be faster with some engines.

Ignacio Vazquez-Abrams
And better describes the intention.
Michael Krelin - hacker
+1  A: 

Some say it's more efficient to use COUNT(*) it also depends how often are you going to count this value and how often is it going to change. May be it's better to cache it somewhere.

habicht