[UPHPU] get count of database records matching criteria

David Smith DavidSmith at byu.net
Sat Feb 26 11:02:49 MST 2005

<quote who="Gary Thornock">
> SELECT COUNT(age) will be a little faster (or a lot faster, if the
> table is very large).  SELECT COUNT(1) may be even faster than that.

Is it not true that SELECT COUNT(age) will only count those rows where the
age column is NOT NULL?

Also, a little trick I do to make the reading of the count results easier
on the PHP side is this:

  SELET COUNT(age) AS count FROM table

Then, in the PHP code:

  $row = mysql_fetch_assoc( $result );
  $count = $row[ 'count' ];

Which in my view is a tad cleaner than:;

  $count = $row[ 'COUNT(*)' ];

Also, don't forget to error check:

  if( mysql_num_rows( $result ) == 0 ) {
       // error handler here

While we're at it, has anyone had luck with SQLite? I'd be curious to know
if it holds up as well as MySQL under heavy load. Feel free to start a new
thread to respond.


More information about the UPHPU mailing list