Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot unserialize object after storing it serialized in database

I'm trying to store a complex object here and am doing that by serialising the object running a mysql_real_escape_string on it and inserting it into a mysql database.

However when I retrieve it running a sql query - I'm using Zend frameworks Zend_DB_Table here but anyway - and when I try to stripslashes and unserialize I dont get my object back. I've tried to just unserialize without stripping slashes and all but nothings working.


UPDATE

This is weird. I made a simple page which just unserializes a serialised object. If I take the serialized string as it is retrieved from the database and unserialize it via this other page which just has an unserialize() on it - it works perfectly and I get my object back. However in the code where ironically I'm retriving the string and I run the exact same unserialize option there ,its not working!

So basically there is nothing wrong with the serialized string - for some weird reason it won't unserialize it in my application but it unserializes somewhere else, it makes no sense.

like image 877
Ali Avatar asked Dec 01 '22 12:12

Ali


2 Answers

You probably need to run it through base64 encoding first:

$safe_string_to_store = base64_encode(serialize($data));

Then to get it back out:

$date = unserialize(base64_decode($safe_string_to_store));

Try that and let us know if it works.

(and dont run stripslashes on it - there is no need to)

like image 117
ae. Avatar answered Dec 03 '22 01:12

ae.


You shouldn't run stripslashes on it - the database will give you back the right string to put into unserialize.

Make sure you have notices turned on and echo the string before you unserialize it - does it look right?

like image 29
Greg Avatar answered Dec 03 '22 02:12

Greg