PHP unserialize() Error at Offset

AnthonyCL picture AnthonyCL · Jun 27, 2013 · Viewed 7.4k times · Source

The exact error I am getting is:

ErrorException [ Notice ]: unserialize(): Error at offset 5 of 59 bytes

The serialized data returned from a TEXT field in MySQL is (Encoding: utf8, Engine: InnoDB):

a:1:{s:12:"display_name";s:6:"Foo";}

After unserializing the output is:

array(1) { [0]=> bool(false) }

I am expecting something like this:

array(1) { ["display_name"]=> string(6) "Foo" }

I am using FuelPHP 1.6.1, PHP 5.4.10, MySQL 5.5.29, InnoDB 1.1.8 on Mac OS 10.8.4. Some of the serialized entries work on unserialize while others don't, if I copy one that works and paste into one that does not it shows the same error. I believe its a character issue and I have tried to encode into utf8, urlencode, and stripslashes but nothing seems to work.

Any help is appreciated!

Update

The serialized string had a type, I did this when pasting it here for privacy to the actual display name. Here is the actual string:

a:1:{s:12:"display_name";s:3:"Foo";}

Thanks to @robw for pointing this out.

Update & Answer

I am running the serialized data through html_entity_decode() now as such:

$unserialized = unserialize(html_entity_decode($serialized, ENT_QUOTES));

Thank you for the help and advice!

Answer

Rob W picture Rob W · Jun 27, 2013

Your problem is here: s:6:"Foo"

Foo is not 6 characters long... so that will never parse as expected.

Try: s:3:"Foo"

PS: This looks like you edited data in MySQL, then when your application tried to parse it, it errored out. You should never edit a serialized value in the database unless you're 100% sure you've modified the LENGTH in accordance to the TYPE and VALUE.