1

I want to store array [3,9,21] in database (mySQL).Other than saving the values of array, I want to save the whole array in the database. Is it possible?

2
  • 3
    not directly because mysql doesn't have arrays. You wll have to save it's string representation . Besides it's almost always better to normalize he table and store each value as a separate row. Commented Aug 9, 2016 at 18:20
  • Take a look at the serialize method. Commented Aug 9, 2016 at 18:55

1 Answer 1

5

If you are using MySQL 5.7+ you can; it introduced a JSON data type https://dev.mysql.com/doc/refman/5.7/en/json.html

A quick read about the changes: http://lornajane.net/posts/2016/mysql-5-7-json-features

PS -- I'm a fan of the comment above -- storing values as separate rows instead of as arrays is a better option

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

2 Comments

If you need to reference these values in a JOIN or a WHERE clause, yes, you absolutely do need to break them out into a proper relational structure. That's not always the case, and sometimes serializing as JSON is actually more convenient.
You should store each item as a row. Keep your data organized will be much cleaner.

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.