TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-26-2010, 10:22 AM   #1 (permalink)
The Visitor
Newcomer 
 
Join Date: Sep 2010
Location: India
Posts: 1
Thanks: 0
aahwaan is on a distinguished road
Default How do I count the number of rows with certain data

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...
aahwaan is offline  
Reply With Quote
Old 09-27-2010, 02:15 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

You're not using a good database design, it is bad practice to require significant data processing from the results of your database. SQL can easily handle this, here is a quick table structure for it:

Users
user_id | user_name
1 | Jyoti
2 | Preeti
3 | Nyasa
4 |Nini
5 | Pinku

user_cities
city_id | city_name | city_user_id
1 | Mumbai | 1
2 |Delhi | 1
3 | Delhi | 2
4 | Kolkata | 2
5 | Mumbai | 3
6 | Boston | 3
7 | NewYork | 3
8 | Delhi | 4
9 | Chennai | 4
10 | Pune | 5
11 | Mumbai | 5

Now to find the users from Delhi
Code:
SELECT COUNT((SELECT city_user_id FROM user_citiesWHERE city_name='Delhi' GROUP BY city_user_id)) AS cnt
Basically this query runs a count on a subquery. The subquery selects all the rows that are in that city, but eliminates any duplicate user IDs in case one listed multiple times.

I would advise you to become more experienced with SQL as a separate subject from PHP. Knowing good database practice will do you good when working with data driven applications. Advanced SQL is also pretty cool, data manipulation is fun.
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
feedback on my class please frostyboy33 Advanced PHP Programming 7 10-22-2012 09:12 AM
How to get row number from 2 top numberd rows ttmtake General 5 01-28-2009 11:16 PM
Venerable methods and the applications they are commonly trusted in. Village Idiot Tips & Tricks 7 11-06-2008 07:36 AM
how to get the rows affected(not number of rows) kororo203 Absolute Beginners 2 04-23-2008 08:32 AM
Tips: PHP security Village Idiot Tips & Tricks 22 11-23-2007 11:17 PM


All times are GMT. The time now is 02:43 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design