RE: [PHP] counting number of records in a MySQL table; how do I get
| From: | Jay Blanchard | Date: | Thu, 03 Oct 2002 14:58:28 +0000 |
| Subject: | RE: [PHP] counting number of records in a MySQL table; how do I get | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-118755@lists.php.net to get a copy of this message | ||
[snip]
> $result = mysql_query("SELECT * FROM tbl");
> $number_of_rows = mysql_num_rows($result);
>
> You don't have to do the count in the SQL, as John said earlier a SELECT
> COUNT(*) FROM tbl will always return one row, even if the value of the row
> is 0. Of course you could always test for that.
>
> CountingRows Solution #317 :^]
That's a really ineffecient solution, though. COUNT() of optimized to return
a quick result. Why would you select everything and use up all of those
resources to just get a count of the rows?
[/snip]
I was thinking along the lines that there needed to be a query that returned
some results that may also be displayed or used within the script. For
instance, I use a paging script that queries for certain conditions and
returns 40 records per page. I also display a line that says, "There are x
records based on the criteria that you have selected.". x comes from
mysql_num_rows. Now, if all I had to do was get the number of records I
would use sql COUNT and test for the condition by seeing what number was
returned by the COUNT.
HTH!
Jay