Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 7 of 7

This is a discussion on Array serialization/unserialization in the Shared & Semi-Dedicated forum
So, I commonly use serialize() and unserialize() to enter and extract arrays from my db. I've never had any problems entering the data, but just ...

  1. #1
    JPC Member
    Join Date
    Jul 2002
    Posts
    47

    Array serialization/unserialization

    So, I commonly use serialize() and unserialize() to enter and extract arrays from my db. I've never had any problems entering the data, but just recently, strange results have been occuring when I try to unserialize the serialized array from the db. First off, here is the array in serialized form:

    a:1:{i:1089154742;a:3:{i:0;s:6:"Blaser"; i:1;s:14:"ImAWannabeOgre";i:2;s:410:"Our thieves assassinated 180 enemy wizards!
    Our thieves have returned with 78,835gc.
    Our thieves were able to steal 45,150 runes.
    Our thieves were able to steal 35,685 runes.
    Our thieves were able to steal 28,203 runes
    Our thieves were able to steal 22,291 runes.
    Our thieves were able to steal 17,618 runes.
    Our thieves were able to steal 13,925 runes.
    We have converted 553 enemy soldiers to our army.";}}

    Now, here is the error I get:

    Notice: unserialize(): Error at offset 489 of 499 bytes in c:\root\web\utopiapimp\array.php on line 14

    all that happens on that line is
    PHP Code:
    echo htmlentities(print_r(unserialize($_POST['data']),True)); 
    where $_POST['data'] is the serialized array listed above.

    Normally, I would understand the above error, but I can't see what error in the serialized text it's referring to. since no manipulation occurs on the text between entering and retrieving it from the database (other than addslashes() on entry to create a valid mysql statement).

    I can't really figure out where the error in my code is, but I figured I might get a hint if I can figure out what error the Notice is referring to in the serialized text.

    Thanks in advance!

  2. #2
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    It sounds as though there is something at character 489 of the serialized string that shouldn't be there. Perhaps an extra line break or null character was added somehow?

    Since this is a notice as opposed to an error you might be able to get by with just surpressing it. Does the script work correctly even though this messages is coming up? If so, try adding an @ before the unserialize() statement (ie @unserialize($str);). In php an @ before any function tells PHP to not display any error messages thrown by that function. I've seen notices like this pop up in PHP functions after upgrades have been made in the past. I'd bet that this issue always existed but PHP is just now being a little more picky. That's usually how it goes.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  3. #3
    JPC Member
    Join Date
    Jul 2002
    Posts
    47
    Nah, it script crashes and burns. Or rather, it doesn't unserialize anything, and having that data as an array is vital to the script.

    What I don't get is byte 489. It's in the middle of a sentence, and nowhere near a line break. If a null character got in there somehow, it was an act of God. I don't think that's the case, however. The string (inside of the array) that it's complaining about is 499 bytes, by my count, so it seems that the data is of the proper length..

  4. #4
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,306
    Before you bang your head on the wall about this, you might want to just retype the lines into a text editor and re-import it into the database, just for the heck of it.

    Also, I saw a refernce to using stripslashes() in the manual page, and the author says "I have no idea how the slashes got in there in the first place" or something like that:

    http://us3.php.net/manual/en/function.unserialize.php

    http://us4.php.net/manual/en/function.serialize.php

    g'Luck

  5. #5
    JPC Member
    Join Date
    Jul 2002
    Posts
    47
    Thanks for the replies.

    I've already pored over serialize and unserialize's doc pages, without anything that seems to refer to this.

    I tried retyping the last line or two of the offending array text, and tried tossing it back into a little script that simply unserialize()'s what gets sent to it from an html form.

    If you look at my original post, it said '489 of 499 bytes', so that surely has to be somewhere near the end of the data. After knocking off and retyping at least 60 chars, it stays the same with the error.

    I thought to myself that maybe if I just shorten the string being saved inside of the array, I could find what 'character/byte' it's complaining about. I removed the last 60 or so characters from the string, and adjusted the string length value within the array. The bytes referred to by the notice seem to ...move with the removing text. If it was originally 489 of 499, it will shorten to 459 of 499 if I remove 30 or so bytes.

    (Oh, and for the record, I did try using stripslashes(). It didn't do anything, as I suspected, since there are no visible slashes in the data.

    Hoooow...frustrating . The site I pulled this data from is one on which I recently published a large number of changes, so common sense would say 'something you changed is causing this'. I don't believe I touched any of the code that messes with these particular arrays, and so it's hard for me to pick a spot in my code without finding out what that error *really* means

  6. #6
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,306
    Dude,

    Some suggestions...

    Break it down into 8 or 10 pieces, and type it all in, and add a piece line by line. Sometimes errors are reported at the end of the data when they're somewhere else.

    Have you made an irreducible piece of code to replicate this error? (Acquire the data (in variables would be ok) serialize the data, unserialize the data. If that works, include inserting/reading the data from the database.

    If you had done this, you may have found your problem, or at least isolated it, by now.

    You'll find out if it's because of the other data, the length, the combination of things. This is the brute force method that you've gotta use when you're stuck like this.

    If you make the irredcible code and then publish it here, others who might care to help might give it a whirl, too.

  7. #7
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    Have a look at http://bugs.php.net/bug.php?id=24063. I'm too tired to make sense of it right now, but it looks like this might be a PHP bug.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

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
  •