MySQL Forums
Forum List  »  Newbie

How do I count the number of rows with certain data
Posted by: Preeti Priyadarshani
Date: September 26, 2010 04:04AM

I have a database with the following fields

id (primary id)
name (name of member)
city (cities where he has lived, comma separated values)

I have this sample database
id | Name | City
1 | Jyoti | Mumbai, Delhi
2 | Preeti | Delhi, Kolkata
3 | Nyasa | Mumbai, Boston, NewYork
4 |Nini | Delhi, Chennai
5 | Pinku | Pune, Mumbai

Now I wish to know how many members are from "Mumbai"

$result = mysql_query("SELECT * FROM members");
$row = mysql_fetch_array($result);
$city = $row['city'];
//now i explode the city to find values in array
$array = explode(",",$city);
Then see individual results. Then I used
if(in_array('Mumbai' $array)){
echo $value;
}

This gives me the names
Jyoti
Nyasa
Pinku

How Do I count the numbers of these, I mean how do I reach at a result of '3'. Anyone please help me.

N.B. I tried to use count() for the query that failed me. I do not know what other method is there since I am new to Php MySQL.

Please help me...



Edited 1 time(s). Last edit at 09/26/2010 04:10AM by Preeti Priyadarshani.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.