2

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!

AnthonyCL
  • 21
  • 1
  • 5
  • 1
    You do need to provide a hexdump of the actual database contents, not just the printable copy&paste excerpt thereof. Else nobody can tell where the encoding problem lies. – mario Jun 27 '13 at 16:31
  • It is not serialized properly a:1:{s:12:"display_name";s:3:"Foo";} would give you the answer you want. – Anigel Jun 27 '13 at 16:31
  • @mario Is this what your looking for? "613A313A7B733A31323A22646973706C61795F6E616D65223B733A333A22466F6F223B7D" – AnthonyCL Jun 27 '13 at 16:40
  • 1
    `I am running the serialized data through html_entity_decode()` So you are breaking it intentionally and asking for help? – dev-null-dweller Jun 27 '13 at 17:04
  • No, that's not what I meant. I meant the **raw data**. Not yet another encoding of the visible copy&paste text we already knew about. You obviously applied html escaping prior to storing your data into mysql. – mario Jun 27 '13 at 17:55

1 Answers1

3

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.

Rob W
  • 9,134
  • 1
  • 30
  • 50
  • 2
    I would suggest that the fastest safe way to edit serialized data is to unserialize it, edit it then re-serialize it to make sure it is always valid data – Anigel Jun 27 '13 at 16:33
  • 1
    If there's serialized data in the database, chances are the user didn't do this themselves. I agree, however, some people may not realize this until it's too late! :( – Rob W Jun 27 '13 at 16:34
  • Apologies, I changed the name to Foo for privacy, it was 6 characters in length. The error still occurs however. – AnthonyCL Jun 27 '13 at 16:37
  • Ah. In that case we'd really need to see the full string. You can replace values for us as long as the content length matches up. – Rob W Jun 27 '13 at 16:40
  • @RobW The updated string is: `a:1:{s:12:"display_name";s:3:"Foo";}` and the hexadecimal from MySQL is `613A313A7B733A31323A22646973706C61795F6E616D65223B733A333A22466F6F223B7D ` – AnthonyCL Jun 27 '13 at 16:43
  • Your MySQL string is being stored as a hexadecimal (literally)? – Rob W Jun 27 '13 at 16:47
  • @RobW no someone asked for the hex string because perhaps there is a bad character. Just provided for reference. – AnthonyCL Jun 27 '13 at 16:48
  • There's something we're not seeing from you then. Ignore hexadecimal values - we only work with literals in PHP. See: http://phpfiddle.org/main/code/6p7-nkz – Rob W Jun 27 '13 at 16:50
  • You are not storing an object in there, are you? – WanWizard Jun 27 '13 at 16:51
  • Get rid of html_entity_decode - it doesn't belong there. That will fix it! – Rob W Feb 01 '14 at 19:01