How do I store an array in a database as an array?

Discussion in 'PHP' started by Imozeb, Apr 2, 2010.

  1. #1
    How do I store an array in a database as an array so when PHP pulls the data out of the database it will be treated as an array?
     
    Imozeb, Apr 2, 2010 IP
  2. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #2
    you can serialize it before storage and revert its serialization after retrieval using the functions serialize() and unserialize()
     
    nabil_kadimi, Apr 2, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So, if I wanted to check whether text exsits in my database I would do something like this?

    PHP code:
    
    //get data from database as $data which is not an array
    $data = unserialize($data);
    
    //check if in array
    if(in_array($data, 'bob') == 0)
    {
       echo('bob does not exsist!!!');
    //serialize for database
       serialize($data);
    //insert $data into database here
    }
    else
    {
      echo('bob exists!!!  Hi bob!!!');
    }
    
    
    Code (markup):
     
    Imozeb, Apr 2, 2010 IP
  4. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #4
    i agree with the post above, if i understood what you meant correctly, serialize it before storing then unserialize it when you fetch it from the db
     
    creativeGenius, Apr 2, 2010 IP
  5. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #5
    i just dont know why must you store it this way, you can easily create a table for accounts where you can list each of the names? its more efficient
     
    creativeGenius, Apr 2, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I need to for IP addresses which refer to specific rows.

    Please help me.
     
    Imozeb, Apr 2, 2010 IP
  7. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Why don't you just create a "ip address" column in your SQL database?
     
    ThomasTwen, Apr 2, 2010 IP
  8. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I did create an ip address column in my MySQL database now I want to populate it with IP addresses (each row corosponds to a specific name and each name has a list of Ip's). It is really hard to explain. I think this is the simpilest way of doing what I want, so can you please if this code will work?

    PHP code:
    //get data from database as $data which is not an array
    $data = unserialize($data);
    
    //check if in array
    if(in_array($data, 'bob') == 0)
    {
       echo('bob does not exsist!!!');
    //serialize for database
       serialize($data);
    //insert $data into database here
    }
    else
    {
      echo('bob exists!!!  Hi bob!!!');
    }
    Code (markup):
     
    Last edited: Apr 2, 2010
    Imozeb, Apr 2, 2010 IP
  9. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You could store each IP in a new row with it's identifier being the user's name. That way you can pull a list of IP's and build the array in PHP.
     
    Brandon.Add.On, Apr 3, 2010 IP