unserialize(): Error at offset

Posted on: March 14th, 2014

While working on saving an array of data into a WordPress meta value, I learned about the wonders of serialize() and unserialize() which let you save an array of data to your database and then pull it out of the database in a nicely structured format.

Simply it takes this:

Array
(
    [0] => Array
        (
            [company] => Max Foundry - Internet
            [title] => WordPress Developer
            [summary] => - Create Premium Themes for the WordPress world - Primary support for MaxButtons, MaxGalleria and MaxInbound - Produce screencasts highlighting theme capabilities - Write "This Week in WordPress" and other blog posts about current trends or topics related to WordPress - Design logos and other elements
        )

)

and makes this:

a:1:{i:0;a:3:{s:7:"company";s:22:"Max Foundry - Internet";s:5:"title";s:19:"WordPress Developer";s:7:"summary";s:308:"- Create Premium Themes for the WordPress world - Primary support for MaxButtons, MaxGalleria and MaxInbound - Produce screencasts highlighting theme capabilities - Write "This Week in WordPress" and other blog posts about current trends or topics related to WordPress - Design logos and other elements";}}

Pretty snazzy right? Well yes if the content you’re going to put in is controlled by you and you understand what can and can’t go into it.

When unserializing the data with unserialize() I cam across this error. “unserialize(): Error at offset 426 of 428” WAT. Why isn’t that working properly. To the Google Machines! After rooting around on stack overflow and seeing varying reasons, I came to the conclusion that it was because of the quotation marks hiding in my input. Notice “This Week In WordPress” is in quotes? Yeah, that bones you in terms of unserializing, because your machine becomes confused. It sees that first quotation mark and thinks, “oh, well that’s the end of that piece, let’s move on shall we?” Meanwhile, the rest of your array gets stuck somewhere in data land and errors are thrown.

moss-throw

Now that we’ve fumed long enough, let’s find the solution. It all happens when you first serialize your information. By serializing without any manipulation, you’re going to save all the quotation marks and create quite the kerfuffle. But with a little help from mysql_real_escape_string() you too can save your data properly.

$arrayToSave = $_POST['your_array'];
$serializer = serialize($arrayToSave);
$breakfast_serial = mysql_real_escape_string($serializer);

// I'm using this in WordPress so...

wp_update_user(array(
  'place_for_array' = $breakfast_serial
));

// information saved

So now that you’ve escaped everything and cleaned up that input, you can unserialize() with ease and then display your array. Only problem is, now you have extra \” when it should just be ” and rn where you want a <br />. Not sure about the best way to fix that yet. More to come in….PART 2. Dun dun dunnnnn.

previous post: Why I Finally Paid For a Template – And Why I Won’t Do it Again next post: array_filter() is Your Friend