I would like to get a result that is the total number of Occurrences in a column. SELECT * FROM mydata WHERE pet=monkey Will show me all the monkey results in the pet column,but how do I get a result like 6,if there are 6 results for monkey ?
<?php $q="select * from table where pet='monkey'"; $res=mysql_query($q); $count = mysql_num_rows($res); echo $count; ?> PHP:
If you are using .net and bind the result to a datalist, you can use datalistvar.Items.Count This way you don't need to make 2 separate queries. I can get the results count in PHP using the returned array length property I think, without having to make 2 queries.
Thanks technoguy worked great,I have something to build on now to learn how to do more math on the database.
Using the "AS" keyword you can name the value returned by count. select count(*) as numPets from table where pet=monkey Code (markup): If you want to know the number of pets for more than one pet you write select count(petID) as numPets, pet from table group by pet Code (markup): Each row in the result will have the name and count for the pet.
How would i do this if I had 3 columns: (pet,animal,zoo) and I wanted a total for monkey from all 3 columns ?
<?php $q="select * from table where pet='monkey' || animal='monkey' || zoo='monkey'"; $res=mysql_query($q); $count = mysql_num_rows($res); echo $count; ?> PHP: you are requested to add rep. thanks