0

I have some questions about handling with array concerning storing in mysql.

I stored an array (of numbers) in my mysql database as text Like: COLUMN_ARRAY: [2,3,4,5,6,7]

( including [ and ] )

  • My first question: is this a good method or can I work with that for my further steps? or are there any other better methods for storing array in sql column?

  • My second question: how could it be possible to pick every single element, or go throu all elements in the array with php?

because I want to make a query like if COLUMN_ARRAY includes the number 3 then do blablabla...

maybe you can give me some hint, idea, link or tutorial :)

EDIT:

Ok I did some change and changed everything to something like that:

How to store arrays in MySQL?

the only problem is now: how can I handle with an array (like [1,2,3]) in php - to handle with every single element in that?

Community
  • 1
  • 1
Steve
  • 127
  • 1
  • 9
  • 12
    Don't do that. Store each number in a row of a related table. – AbraCadaver Dec 09 '14 at 21:36
  • 1
    If you really MUST put all the values in a column, don't put any extra characters like `[]` or space in it. Then you can use `FIND_IN_SET` to match values in the list. – Barmar Dec 09 '14 at 21:38
  • [Nice tutorial here](http://www.phpknowhow.com/mysql/many-to-many-relationships/) for `many-to-many` database relationships. – showdev Dec 09 '14 at 21:39
  • 1
    ... but you'd better read about Normalization, e.g. for a start http://en.wikipedia.org/wiki/Database_normalization – Hartmut Holzgraefe Dec 09 '14 at 21:40
  • thank you, I think I will go the "related table" way - but even I do it I have still the problem from my second question... i am transfering an array to my .php (containting all the numbers...) how can I take every element in the array with php? because I have to do it - to store them in the related db – Steve Dec 09 '14 at 21:41
  • If you really want arrays, and you probably don't, [Postgres](http://www.postgresql.org/) supports these as a native data type and they can be indexed and queried quite easily. – tadman Dec 09 '14 at 21:41

0 Answers0