Forum Moderators: coopster

Message Too Old, No Replies

php serialize / unserialize problem

         

willg825

2:32 am on Jan 3, 2005 (gmt 0)

10+ Year Member



Hi Everyone -

I am having an interesting problem. For various reasons, I need to store an 2d array object in a mysql database field. I've been using serialize() to store it, unserialize() to retrieve it, edit it, then serialize() to store it again, and everything has been working with no problem for a week.

Today I started getting: "
Fatal error: Cannot use string offset as an array" error messages. It points to a place where the unserialized() 2d array is accessed. After much debugging, i've realized the problem is that unserialize() isn't working - its only returning the serialized object to the string. No error, no '0' or FALSE - just a string representation of the serialized() 2d array. This is, of course, causing serious problems. Here is a code snippet of one of the problem spots:

unset($prim_array);unset($sec_array);
$prim_array = unserialize($row[prim_approvers_array]);
$sec_array = unserialize($row[sec_approvers_array]);

when I print_r() the prim_array and sec_array, I get a string of the serialized() form. I've tried print_r(unserialize(function etc)) and I've tried without the unset()s, all the same problem.

My guess right now is that somehow the data got corrupt in the table - but then unserialize() should return false according to the docs. Has anyone any idea what might be happening? It worked for a while, now it doesnt, and it is frustrating.

I appreicate everyones help.

-will

dmorison

6:21 am on Jan 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the data you are serialising has come from a form POST you may have a slashes problem. Make sure that you are using stripslashes() [uk2.php.net] if necessary.

This could also explain why the problem is sporadic - as slashes would only be added to a POST'ed value if neccessary, such as when apostrophe's are used.

willg825

2:30 pm on Jan 3, 2005 (gmt 0)

10+ Year Member



dmorison,

thanks for the reply. i think you may be on to something - thinking back now, I am pretty sure the problem began after I tried addeding a $_POST value to the array.

Should I run stripslashes() on everything before serialize() only? or do I need to run it again after unserialize? (i.e, stripslashes(unserialize()) or unserialize(stripslashes()))... and are the arrays that are corrupt beyond repair?

thanks!

-will