Skip to content
Advertisement

How to store a array in a database?

I am trying to learn php databases and I have a question. How do I store an array in a database? I saw an answer on stackoverflow and there were they doing something with type double and in an other answer they were talking about creating a table for every user but I can’t find a solution.

So summarized. I want to store an array in a database. I have acces to phpmyadmin so from there can I set the value type. and I would like to store that array in one column.

Can somebody help me solving the problem?

edit one: The things I want to store are music tags. So in code it would be something like this:

 array('pop','rock','country');

I want to store It in one column to make it easy searchable

Advertisement

Answer

Usually you shouldn’t store arrays in a single column in a db. It is preferable to have a table with tags and another one that links your entity with its tags. So, before try to store an array in a table just think if it is really the right thing to do in your specific case (usually not).

If you are sure you want to do that then you have serialize and unserialize functions.

UPDATE (after five years) serialize and unserialize methods are inherently unsecure and should be avoided. It is preferable to store data in JSON format using json_encode and json_decode.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement