Is serialize the best way to do this?
If, say, I have a database of music and I want to classify music by genres. If I allow for multiple genres for a given album, do I store them as an array and serialize them? Like this?
$array = serialize(array(1,2,3)); // numbers are IDs of genres
I get that from another post here. I know I can unserialize them. What I don’t get is how I would write an SQL statement that would retrieve some or all of the data. What would my SQL statement look like if I wanted to retrieve all of the genres for a given album?
You really want to normalise the data and store the genres and the album to genre association in different tables.
i.e.: You’d ideally have three tables:
By doing this you’ll be able to trivially add/remove, etc. genres as required in the future and will be able to perform lookups by joining the tables rather than having to perform string manipulation.