Group_concat and then Exploding it into an Array

Go To StackoverFlow.com

3

I am group_concat a bunch of reviews from my db which turns it into something like this:

This is review 1, This is review 2

then I explode it into an array like:

$review = $row->review;
$row->review = explode(',', $review);

This works great until someone in there review adds a , then it obsoulvy no longer works. So is there a way to group_concat and put something besides a comma between them? Is there a better way of fixing this? Any ideas would be greatly appreciated.

2012-04-04 23:48
by Jacinto


2

Yes, don't use GROUP_CONCAT. Create a subtable containing the reviews and appropriate relationships that link each review to.. whatever entities you are dealing with.

If you already have it set up like this, and are just using GROUP_CONCAT to fetch multiple reviews into a single result set... then still don't use GROUP_CONCAT. Use a subquery.

2012-04-04 23:51
by Chris Laplante
Yeah I figured, ok I will just change it. Thanks for the help - Jacinto 2012-04-04 23:56
You're welcome - If you are interested, I'd recommend this book (which taught me the evils of GROUP_CONCAT and friends): http://www.amazon.com/Beginning-MySQL-Database-Design-Optimization/dp/1590593324/ref=sr11?ie=UTF8&qid=1333583845&sr=8- - Chris Laplante 2012-04-04 23:57
but group_concat can be so cool..... haha. I like to use it sometimes for a quick query when i need to grab a bunch of id' - Ascherer 2012-04-05 00:03
Thanks! Just got the book - Jacinto 2012-04-05 00:22
@Ascherer: It certainly has its uses. Just not as a replacement for a table : - Chris Laplante 2012-04-05 00:22
@Jacinto: I think you'll like it. Side note: It actually does have a real cover. Not sure why Amazon only shows the manuscript cove - Chris Laplante 2012-04-05 00:23
@SimpleCoder: do you know of a good book that dives into MVC. BTW got the book today and love it already 50 pages into it - Jacinto 2012-04-06 21:12
Glad you like the book. Unfortunately, I haven't been shopping for MVC books for a while now. The frameworks change so often. I'd recommend doing the tutorials for each of the major frameworks (Zend, CakePHP, Symfony, CodeIgniter, etc.) to see which one you like best - Chris Laplante 2012-04-08 22:04
Ads