Counting items in a database
Moderator: General Moderators
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
Counting items in a database
Hi everyone.
I want to count the amout of items in my MySQL DB where a certain column has a value of 'yes'. I understand I need to use a while() command. How would I use it?
Thanks.
I want to count the amout of items in my MySQL DB where a certain column has a value of 'yes'. I understand I need to use a while() command. How would I use it?
Thanks.
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
-
fractalvibes
- Forum Contributor
- Posts: 335
- Joined: Thu Sep 26, 2002 6:14 pm
- Location: Waco, Texas
only to make it look more like php (but the magic is still sql, you have to query mysql in sql anyway
)if you want to query the rows anyway you might do so with
Code: Select all
$query = 'SELECT Count(*) FROM Mytable';
$result = mysql_query($query, $dbConn) or die($query. ': '.mysql_error());
$numRows = array_pop(mysql_fetch_row($result));Code: Select all
$query = 'SELECT * FROM Mytable';
$result = mysql_query($query, $dbConn) or die($query. ': '.mysql_error());
$numRows = mysql_num_rows($result));
...
while($row = mysql_fetch_array($result))
{
echo ...
}-
fractalvibes
- Forum Contributor
- Posts: 335
- Joined: Thu Sep 26, 2002 6:14 pm
- Location: Waco, Texas
-
laserlight
- Forum Commoner
- Posts: 28
- Joined: Wed Jan 01, 2003 6:41 am
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
for SELECT count(...) the use of * it doesn't matter (in fact it might be even a bit faster, negligible) but for SELECT * FROM ... it does matter even if you're only going to use mysql_num_rows(). At least since mysql_query() uses mysql_store_result by default to transfer the results instead of mysql_use_resultI think volka intended that example for counting all the rows in the table, in which such a condition wouldnt apply.
Naming the fields you want to select is almost always a good idea.
But yes, it was only an example and I didn't take too much care