问题描述
我试图在此处存储一个复杂的对象,并通过序列化在其上运行mysql_real_escape_string
的对象并将其插入到mysql数据库中来实现.
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.
但是,当我运行SQL查询来检索它时-无论如何,我还是在这里使用Zend
框架Zend_DB_Table
-当我尝试除斜杠和反序列化时,我没有找回我的对象.我已经尝试过反序列化而不去除斜杠,但几乎没有任何作用.
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.
更新
这很奇怪.我做了一个简单的页面,只是反序列化了序列化的对象.如果我从数据库中检索序列化的字符串,然后通过另一页对其进行反序列化,则该页面上只有一个unserialize()
-它可以完美工作,并且我可以将对象找回来.但是,在具有讽刺意味的是,我正在检索字符串的代码中,我在那里运行完全相同的反序列化选项,它无法正常工作!
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.
推荐答案
您可能需要先通过base64编码运行它:
You probably need to run it through base64 encoding first:
$safe_string_to_store = base64_encode(serialize($data));
然后将其取回:
$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)
这篇关于将对象序列化存储在数据库中后无法反序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!