troubleshooting Question

Help exploding and count values.

Avatar of jej07
jej07 asked on
Web DevelopmentPHPMySQL Server
2 Comments1 Solution338 ViewsLast Modified:
I have MySQL database fields that can contain up to 3 values, separated by a comma (Red,Blue,Yellow). I would like to count how many times each value appears. So, the results would be Red 4, Blue 3, Yellow 2.

id      | color
1      | Red,Blue
2      | Red,Blue
3      | Red,Yellow
4      | Red,Blue,Yellow

It seems like I would have to loop through each row, exploding the string; then loop through the results to count the values. But I'm not even sure where to begin doing that.

$result = mysql_query("SELECT color FROM cars");
$row = mysql_fecth_array($result);

$favColors= explode(',',$row['color']);

$count = count($favColors);
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros