Google Answers Logo
View Question
 
Q: Specification of PHP's serialize() ( Answered 4 out of 5 stars,   2 Comments )
Question  
Subject: Specification of PHP's serialize()
Category: Computers > Programming
Asked by: shane43-ga
List Price: $5.00
Posted: 02 Aug 2003 22:58 PDT
Expires: 01 Sep 2003 22:58 PDT
Question ID: 238363
The following was inserted into my db as a 2-D array that was
serialized for storage:

a:5:{i:1;a:3:{s:8:"formname";s:5:"Fname";s:10:"commonname";s:10:"First
Name";s:5:"value";s:8:"Mercedes";}i:2;a:3:{s:8:"formname";s:5:"Lname";s:10:"commonname";s:9:"Last
Name";s:5:"value";s:8:"OConnor";}i:3;a:3:{s:8:"formname";s:5:"Phone";s:10:"commonname";s:5:"Phone";s:5:"value";s:12:"818-893-4226";}i:4;a:3:{s:8:"formname";s:5:"Email";s:10:"commonname";s:13:"Email
Address";s:5:"value";s:18:"moandto@seiu99.net";}i:5;a:3:{s:8:"formname";s:6:"Income";s:10:"commonname";s:6:"Income";s:5:"value";s:16:"$3,000
PER MONTH";}}

However, when I try to unserialize it, it won't create the array
again. Most 99.9% of entries are fine, but every once in a while one
gets messed up like this one.

Does anyone know what could be causing this? or...do you know where I
could go to find out the definition of serialization so that I can see
where it differs from the spec?

Request for Question Clarification by joseleon-ga on 04 Aug 2003 01:56 PDT
Hello:
 Does your array values contain escaped characters? Maybe those chars
are not saved correctly to your database.

Regards.

Clarification of Question by shane43-ga on 05 Aug 2003 13:42 PDT
Before putting any data into the array, I remove all single(') and
double(") quotes that are in all strings. Are there other characters
that need to be escaped before I seralize something? I would think
that serialize would take care of that for me...

Request for Question Clarification by joseleon-ga on 05 Aug 2003 23:14 PDT
Hello:
   Serialize convert objects/arrays to strings, but when saving those
strings to a database could cause problems, I will make some tests
with the one you post.

Regards.
Answer  
Subject: Re: Specification of PHP's serialize()
Answered By: joseleon-ga on 06 Aug 2003 01:43 PDT
Rated:4 out of 5 stars
 
Hello:
  I have found what is the problem, let's look at this part of the
array:

5:"value";s:8:"OConnor";}i:3;a:3:{

When php serializes an object, s:8 means a string of 8 characters and
when you remove the single (') quote from that value (O'Connor)
*after* serialize it, the string has just 7 chars, not 8, this is why
unserialize can't recover the string.

A possible solution would be to replace all single(') quotes and
double(") quotes by \' and \" before save the string to the database,
if you post here the piece of code, I will do it for you.

Regards.
shane43-ga rated this answer:4 out of 5 stars
Thanks so much. The cause of the error was exactly as you said,
however I could not get your suggestion to work (but that wasn't my
question...I just wanted to know why, which you answered).

Thanks!

Comments  
Subject: Re: Specification of PHP's serialize()
From: vijayanand-ga on 03 Aug 2003 02:12 PDT
 
http://in.php.net/manual/en/function.serialize.php
Subject: Re: Specification of PHP's serialize()
From: shane43-ga on 03 Aug 2003 10:57 PDT
 
Yeah, I know about that page, but what I am after is the exact
definition of how to serialize something. As in, I need to know what
"a:5:{i:1;a:3:{s:8:" means so that I can see where the above differs
from the accepted format.

Thanks,
Shane

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy