How to store multiple values in a single field of MySQL?

Discussion in 'PHP' started by HiSoC8Y, Mar 1, 2008.

  1. #1
    Hello

    With PHP and MySQL, i would like to store multiple values in a single field in a table. What's the best way to achieve this?

    say while submitting a form, i have a multiple checkboxes where i can select the values, and i want those values to be store into a single field.

    thank you
     
    HiSoC8Y, Mar 1, 2008 IP
  2. Kennedy

    Kennedy Peon

    Messages:
    994
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    seperate them with spaces or something, like so:
    $pieces = "value1 value2 value3";
    when it comes time to get them, put them all in a string and use the explode function:
    $pieces = explode(" ", $pieces );
    This will create an array, so it will now look like:
    $pieces[0] = "value1";
    $pieces[1] = "value2";
    $pieces[2] = "value3";
     
    Kennedy, Mar 1, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Or you can use serialize() / unserialize().
     
    nico_swd, Mar 1, 2008 IP
  4. HiSoC8Y

    HiSoC8Y Active Member

    Messages:
    136
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #4
    Thanks guys,

    Kennedy

    i have to do some research about that function cause i dont know it really...
     
    HiSoC8Y, Mar 1, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    nico_swd, Mar 1, 2008 IP
  6. roy.laurie

    roy.laurie Peon

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you think that you'll ever be querying against this field later on, though, don't shove multiple values into it. Make a mapping table or something.
     
    roy.laurie, Mar 1, 2008 IP