Results 1 to 3 of 3

Thread: [RESOLVED] Counting the number of Rows... efficiently

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Resolved [RESOLVED] Counting the number of Rows... efficiently

    Currently I am using an MySQL query to gather all the IDs pertaining to a specific category and then I use mysql_num_row() to count the number of rows to get the total.

    Is there a more efficient way to do this? I know there is a COUNT() for MySQL but I didn't know how to return that and actually use it in PHP.

    Thanks
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Counting the number of Rows... efficiently

    I believe it is

    PHP Code:
    $count mysql_result(mysql_query('SELECT COUNT(*) FROM `table_name`'), 0); 
    As for efficiency, not sure. Loop each one a few hundred times and benchmark them using microtime() if you're concerned

    Edit: My educated guess is that if you require the table data anyway then mysql_num_rows() will be more efficient than two queries. But if all you need is a row count then this will be more efficient.

  3. #3

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Counting the number of Rows... efficiently

    Thanks!
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width