SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Saving An Array To A DB

    I am attempting to save an ARRAY to a database. I read somewhere that I should use the serialize function to do so:

    PHP Code:

    $array
    [] = array('nickname' => 'ibeblunt');

    addslashes(serialize($array)); 
    The result would be:

    Code:
    a:1:{i:0;a:1:{s:8:\"nickname\";s:8:\"ibeblunt\";}}
    So I stored this value into my DB.

    Now my problem is unserializing it when pulling it from my database.

    I store it in a column named, profile_array in my table, tbl_profile. So when I say:

    PHP Code:

    $array 
    unserialize(stripslashes($profile_array)); 
    It never shows up as an array but as a boolean.

    Any suggestions?
    Last edited by ibeblunt; Aug 10, 2002 at 21:17.
    <///////~

    www.sohh.com

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Saving An Array To A DB

    Originally posted by ibeblunt
    PHP Code:
    $array[] = array('nickname' => 'ibeblunt'); 
    first, you realize that that is creating a multi-dimensional array? e.g. you'd access it as $array[0]['nickname'] and not $array['nickname']. maybe that's what you want, just wanted to point it out.


    PHP Code:
    $array serialize(stripslashes($profile_array)); 
    you need to use the unserialize() function to get the array back. also, as long as you put the string in correctly (and you did by using addslashes()), you shouldn't be using stripslashes() when retrieving it! it could especially be a problem with serialize()'d data, as you may wind up stripping something that needs to be there.
    Last edited by DR_LaRRY_PEpPeR; Aug 10, 2002 at 19:25.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did use unserialize. I had a typo in my code, but it's fixed now.

    To show you EXACTLY what I mean, run this code:

    PHP Code:

    $array
    [] = array('name' => 'Favorite Rapper',
                    
    'value' => 'Redman');
                    
    $array[] = array('name' => 'Favorite Color',
                    
    'value' => 'Red');
                    
    $serialized_ARRAY addslashes(serialize($array));

    echo 
    '<p><b>echo serialized array:</b> ' $serialized_ARRAY '</p>';

    $unserialized_ARRAY unserialize($serialized_ARRAY);

    echo 
    '<p><b>echo unserialized array:</b> ' $unserialized_ARRAY '</p>';

    echo 
    '<p><b>What kind of variable is unserialized_ARRAY?</b> ' gettype($unserialized_ARRAY) . '</p>'
    This is what I get when I run it:

    echo serialized array: a:2:{i:0;a:2:{s:4:\"name\";s:15:\"Favorite Rapper\";s:5:\"value\";s:6:\"Redman\";}i:1;a:2:{s:4:\"name\";s:14:\"Favorite Color\";s:5:\"value\";s:3:\"Red\";}}

    echo unserialized array:

    What kind of variable is unserialized_ARRAY? boolean
    What about you?
    <///////~

    www.sohh.com

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i got the same thing, BUT that's because you didn't insert it into the database and then retrieve it. so in that little bit of test code, you shouldn't use addslashes(). but when you're going to insert it into the database, you do need to use the addslashes() after serialize()'ing the array. then don't use stripslashes() when retrieving, just use unserialize(). it should all work well.

    hope that helps.



    OH! p.s. that may be why you were getting "boolean" in the first post if you HAD stored it in the database, because it may have been stripping stuff that was needed to recreate the array. understand?

  5. #5
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added the slashes to test it. You don't have to retrieve it from a database and retrieve it to see if it works. It's going to come out the same way. DB or no DB.

    Anyway, after doing some research, I found that you have to stripslashes() BEFORE you unserialize()...And it works fine.

    Try changing:

    $unserialized_ARRAY = unserialize($serialized_ARRAY);

    to

    $unserialized_ARRAY = unserialize(stripslashes($serialized_ARRAY));
    <///////~

    www.sohh.com

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *sigh*

    that's what you were doing up there before. let me explain again. you shouldn't have to use stripslashes() ANYWHERE. and IT IS going to be different whether or not you put it in the database.

    1) you're NOT going to put it in a database, like our little test. in that case, DON'T addslashes() (why would you???), just serialize() it. want the array back? unserialize() it. simple as that.

    now, if you screw up and use addslashes() on the serialize()'d array when it's NOT going to be inserted into a database, THEN you do have to use stripslashes() to fix what you screwed up before you unserialize().

    2) you ARE going to put it in a database. in that case, use addslashes(), just as you would for any data, to escape any quotes so it doesn't cause an error. these slashes that are added to the serialize()'d array ARE NOT stored in the database, they're only escape characters. when you retrieve the serialize()'d array it will be EXACTLY (understand? EXACTLY) the way it was BEFORE you used addslashes(). want the array back? unserialize() it. again, simple as that. DO NOT use stripslashes(), this will screw it up.


    got it? no stripslashes() needed anywhere, unless you're not doing something right. try it 100 times, 1,000 times, and you'll see that i'm right.

  7. #7
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dunno.

    Hey, NOW it work - whether I pull it from a database or just run the example. It works.

    Whats the big deal?

    In fact, freddieknowsphp is the person who posted the solution. Do a search on here for unserialize. Even after doing more research on php.net, it's been posted that in order for what I'm doing to work, I have to store the serialized string WITH slashes and when I pull it from the db, I have to strip the slashes first and then unserialize it.

    It works. My question has been solved. The issue is over.

    Thanks.
    <///////~

    www.sohh.com

  8. #8
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, no prob...


    but don't you understand? using addslashes() does NOT STORE the slashes in the database! they're only there for escape characters during the query. i'm telling you, if you use stripslashes() when you retrieve it, it will be broken if there happens to be a \\', \", or \\ in the data. any other number of people on here can tell you that you shouldn't use stripslashes() when retrieving data. that's especially true in this case. you've been warned!

    i just did a search and found freddy's post that i assume you're talking about. basically, it looks like he's wrong. i promise that stripslashes() isn't needed.

    also, i wouldn't trust user comments on php.net (if that's what you mean). anybody that says you need to use stripslashes() when getting data from a DB (as i've seen posted many times) doesn't know what they're talking about.

    one more time people: There are NO slashes that exist that need to be stripped!


    *sigh* geeze...

  9. #9
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, I sure wouldn't second guess the Doc He's proven his extensive knowledge of PHP to all of us

    PS- Larry, have you had a look at the two posts by kevinslair? Any ideas?
    Mike
    It's not who I am underneath, but what I do that defines me.

  10. #10
    SitePoint Addict Kevinslair's Avatar
    Join Date
    Jul 2002
    Location
    Binghamton
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Huh? I missed something.
    I PHP!

  11. #11
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes! the doc is back! excellent!
    - website

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by website
    Yes! the doc is back! excellent! [img]images/smilies/smile.gif[/img]
    You may want to check that the date on that post.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, well, I learned something new today if that's any consolation.

  14. #14
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by freddydoesphp
    You may want to check that the date on that post.
    Damn
    - website


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
  •