Avatar of jej07
jej07
 asked on

Help exploding and count values.

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);

Open in new window

PHPMySQL ServerWeb Development

Avatar of undefined
Last Comment
jej07

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
hielo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
jej07

ASKER
That's not exactly the route I thought I needed to take, but it worked perfectly. Thank you very much for your help.
Your help has saved me hundreds of hours of internet surfing.
fblack61