1

What is the best way to store/retrieve an array in a Mysql database? Is serializing the data the best way?

I am looking for neat, nice way to do this.

6
  • 1
    If you're looking, use the search. As you can imagine, you're not the first one who asks this. Commented Aug 13, 2013 at 7:19
  • What do you want to do with the array once it's in the database? The answer may depend on if you want to look up records based on values in the array or manipulate the array in SQL. Commented Aug 13, 2013 at 7:21
  • Just note that there is no "mysqli database". It is called "mysql" Commented Aug 13, 2013 at 7:22
  • I have to add you really need to search around a bit. There are many suggestions given and the best suggestion I can give is that you read a bit because there are pros and cons and you would need to decide what is best working for you. Part of the topic is whether to normalize or serialize. It depends on what you need (e.g. searching inside the array is not possible when you use PHP serialize). Commented Aug 13, 2013 at 7:23
  • yes json_encode is perfect thanks Commented Aug 13, 2013 at 7:57

1 Answer 1

4

Serializing is actually a worst way ever.

It is like if you cut a door from your expensive car, and use it as a sleigh, only because being too lazy to get some gas. Mysql is an expensive car and it have to be used properly, and solution have to be not a quick and dirty one but out of reason and knowledge.

The solution depends on the nature of the data in this array and most likely will involve a separate table (as tables in mysql being pretty much arrays themselves)

Sign up to request clarification or add additional context in comments.

2 Comments

yes this was my suspicion but not having done much of this since php v4, serialize was the default way to do this.
Great answer, opened my eyes on my problem. I was going to store a list of variables in an array as a single string; and parse the array in PHP. But your saying its better to create a new table and create individual entries and link them to their super?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.