SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot zeeshan's Avatar
    Join Date
    Mar 2001
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to add an array in mySQL?

    Hi all,

    I have built an array and I need to store this array of values (mainly strings) into a mySQL database. I'm using phpMyAdmin so my first question is, what VALUE should I set that field to? Should it be a TEXT field to hold the array?

    And secondly, how do I INSERT this array into the database.

    e.g.
    $values = "'$alias','$pasword','$name','$lastname','$email',0,0,'$gender','$birthday','$occupation','$address','$city',$stateid,$countryid,$incomeid,'$zipcode'::";

    How do I INSERT $values into my info field in TEMPREG table?
    Will it just be a simple INSERT command? And if so, will the info field hold an array of values, or a string separated by commas, or what? I'm confused!

    Thanks.

  2. #2
    This is all Mumbo Jumbo Amit's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    There are few ways by which you can do this ....

    The easiest one (atleast for me) would me to create a TEXT field in your table and then

    $str = serialize( $values );

    and save the $str into your table ...

    And when you want the values ...

    $values = unserialize( $tabledata );

    where $tabledata is the string you get from the table ...

  3. #3
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I stongly recommend u to create 3 tables with fields that u already have in array

    MEMBER TABLE
    ------------
    alias
    pasword
    name
    lastname
    email
    gender
    birthday
    occupation
    address
    city
    stateid
    countryid
    incomeid
    zipcode
    -------------

    COUNTRY TABLE
    -------------
    countryid
    countryname

    -------------
    INCOME TABLE
    ------------
    incomeid
    income
    ------------

    I just saved ur life

  4. #4
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Amit
    Hi,

    There are few ways by which you can do this ....

    The easiest one (atleast for me) would me to create a TEXT field in your table and then

    $str = serialize( $values );

    and save the $str into your table ...

    And when you want the values ...

    $values = unserialize( $tabledata );

    where $tabledata is the string you get from the table ...
    ****, that is a cool trick - I had NO idea you could do that. That will be very useful for a project I'm working on. Thank you!
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  5. #5
    SitePoint Zealot zeeshan's Avatar
    Join Date
    Mar 2001
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot Amit. I'm gonna give that a shot; hopefully it should solve the problem.

    sylow: Thanks for the suggestion, but the problem is that these tables (the entire database for that matter) is already created and populated. Splitting them up now would mean hours and hours of work.
    "Why fix it if it ain't broke!"

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because as your site grows it will become increasingly more important that you keep your data stored in an efficient manner, serializing arrays to store in one field is not efficient and will make your app less scalable in the furute. Of course if don't care about that, then you have nothing to worry about.

    "Why fix it if it ain't broke!"
    All good things are worth doing right the first time.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    This is all Mumbo Jumbo Amit's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess I should have mentioned that this trick would comes at a bit of performance hit ... as freddy mentions but different situations can be handled in many ways ... that is why I said ...


    There are few ways by which you can do this ....

    The easiest one (atleast for me) ...
    I could give couple of more ways, it all depends what you want, you wanna save space, want performance, etc ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •