Re: Storing multiple items in one MySQL field?
| From: | Niel Archer | Date: | Sun, 08 Jan 2012 07:27:21 +0000 |
| Subject: | Re: Storing multiple items in one MySQL field? | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-48143@lists.php.net to get a copy of this message | ||
--
Niel Archer
niel.archer (at) blueyonder.co.uk
> Hello phpers and sqlheads,
> If you have a moment, I have a question.
>
> INTRO:
> I am trying to set up categories for a web site.
> Each item can belong to more than one category.
>
> IE: Mens, T-Shirts, Long Sleeve Shirts, etc.. etc..
> (Sorry no fancy box drawing)
>
> QUESTION:
> My question is what would the best way be to store this in one MySQL
> field and how would I read and write with PHP to that field?
> I have thought of enum() but not on the forefront of what that
> actually does and what it is best used for.
> I just know its a type of field that can have multiple items in it.
> Not sure if its what I need.
>
> REASON:
> I just want to be able to query the database with multiple category
> ID's and it check this field and report back if that category is
> present or if there are multiple present.
> Maybe return as a list or an array? I would like to stay away from
> creating multiple fields in my table for this.
Have you considered separate tables? Store the categories in one table
and use a third to store the item and category combination, one row per
item,category combo. This is a common pattern to manage such situations.
> NOTE:
> The categories are retrieved as a number FYI.
>
> Any help/code would be greatly appreciated.
> But a link does just fine for me.
>
> Best Regards,
>
> Karl DeSaulniers
> Design Drumm
> http://designdrumm.com
>
> Hope your all enjoying your 2012!