问题描述
我在反序列化数据库表中的数据时遇到问题.我将数据序列化并保存到表中.当我检索数据时,我无法正确获取它.下面是我的代码.
I'm facing a problem in unserialize the data from the database table. I'm serialized the data and saved into the table. When i'm retrieving the data i'm not able to get it properly. Below is my code .
$miscel = serialize(array($_POST['Prod_Price'],$_POST['Prod_Cond']));
我成功地将数据插入到数据库中.在数据库表中,它看起来像
I successfully inserted the data into the database. In the database table it looks like
s:38:"a:2:{i:0;s:4:"4444";i:1;s:6:"Middle";}
我如何正确检索数据?
How i can retrieve the data properly?
推荐答案
究竟是什么问题?您应该可以简单地调用 unserialize()
来检索您的数据其原始形式:
What exactly is the problem? You should be able to simply call unserialize()
to retrieve your data in its original form:
// assuming your database column 'foo' contains
// s:38:"a:2:{i:0;s:4:"4444";i:1;s:6:"Middle";}
$miscel = unserialize($row['foo']);
print_r($miscel);
// returns array([0] => 4444, [1] => 'Middle');
如果问题出在正在序列化的数据可读性不佳的事实之内,则应考虑也存储阵列键:
If the problem lies within the fact that the data being serialized is not very readable, you should consider storing the array keys as well:
$miscel = serialize(array('price' => $_POST['Prod_Price'], 'cond' => $_POST['Prod_Cond']));
这篇关于反序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!