unserialize或数组切片?

时间:2012-05-20 10:27:04

标签: php serialization slice

从我的数据库中的此条目获取每个数据块的正确方法是什么?

我正在为wordpress定制一个名为自定义联系表单的插件,基本上对于收到的每封电子邮件,都会在数据库中创建一个新条目,格式如下:

s:8:"enq_name";s:3:"ICE";s:9:"enq_email";s:23:"xvxvxv@hotmail.com";s:9:"enq_phone";s:10:"9191919191";s:11:"enq_message";s:74:"bla bla bla bla and more bla bla bla";s:10:"enq_footer";s:288:"This message has been set to you by my";

如何获取单个值,例如消息enq_message

道歉,如果问题标题中提到的一种或两种方法都是错误的,我不知道如何做到这一点:(

提前致谢

修改 感谢fulhack,我现在有了这个......

$query = "SELECT * FROM `$table_name` WHERE `data_formid` = $ff ORDER BY `$table_name`.`id` ASC LIMIT 0, 5";     
$result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
    echo $myDate = date( 'd/M/Y', $row['data_time'] );
    $string = $row['data_value'];

    $serialized = $string;
    $unserialized = unserialize($serialized);
    $theValue = $unserialized['enq_email'];

    echo $theValue;
    }

但是这只会回应'e'或更多'eeeee',因为数据库中有条目。

1 个答案:

答案 0 :(得分:2)

尝试使用unserialize()(http://www.php.net/manual/en/function.unserialize.php),然后使用密钥(在您的情况下为enq_message)来检索您要查找的值。

编辑:我无法访问PHP安装,但我认为这样可行:

$serialized = serialize(array("key" => "value")); // This is the data you supplied
$unserialized = unserialize($serialized);
$theValue = $unserialized["key"];

你应该添加一些错误检查。