Results 1 to 4 of 4

Thread: [RESOLVED] Question on Mysql count.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Resolved [RESOLVED] Question on Mysql count.

    Anyone know if there's a better approach than doing

    Code:
    $query = "SELECT COUNT(name) FROM members"; 
    	 
    $result = mysql_query($query) or die(mysql_error());
    simply want a count of the rows really and don't need anything returned.

    oh oh oh ... is it possible to do a sort of "COUNT" "WHERE" thing to only count records matching a criteria?

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

    Re: Question on Mysql count.

    (1) The return value of that query is the row count, which is what you want...

    (2) select count(*) from members where a=b

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Question on Mysql count.

    you can use the mysql_num_rows() function to see how many rows resulted from your query, though you can use MySQL's COUNT() function to do add a WHERE clause, too. If you're only looking to return the number of rows (and not any actual data from the query), COUNT() might be faster.
    PHP Code:
    <?php
      $sql 
    mysql_query("SELECT id FROM table WHERE this='that' AND that='this'");
      
    $num mysql_num_rows($sql);
      echo 
    "There were {$num} rows returned.";
    ?>

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Re: Question on Mysql count.

    Thanks dude that works just like a brought one

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