具有unserialize功能的PHP不起作用

时间:2012-12-06 19:02:24

标签: php function serialization echo

我的unserialize()功能有问题。我认为的问题在于这一点:

$rslt = unserialize($data);

并且不回显此命令:

echo $rslt[0]["product_id"];

有什么问题?

<?php
// *****************zima user id********************
$user =& JFactory::getUser();
$usr_id = $user->get('id');

// *****************zima product id i pravi session********************

mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("shopping_katalog") or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");

$result = mysql_query("SELECT * FROM x9qg6_jshopping_cart_temp WHERE id_cookie ='".$_COOKIE["jshopping_temp_cart"]."'");    
$data=null;
while ($row = mysql_fetch_array($result)) 
{
    $data = $row['cart'];
}
$rslt = unserialize($data);
echo $data;

$session = JFactory::getSession();
$session->set('kor', $usr_id);
$session->set('kupid', $rslt[0]["product_id"]);
echo $rslt[0]["product_id"];
$mymessage = $session->get('mymessage');
echo $mymessage;

if (!$result) {
echo 'Проблем со купувањето _php.' . mysql_error();
exit;
}else{
}

我试图用unserialize()函数阅读的文字是:

a:1:{i:4;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196;s:11:"category_id";s:3:"209";s:5:"price";d:1;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:0:"";s:12:"product_name";s:4:"test";s:11:"thumb_image";s:0:"";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}}

2 个答案:

答案 0 :(得分:3)

在反序列化已经序列化并保存到数据库的数据之前,我遇到了麻烦。我发现排序的最简单方法是序列化的base64_encode和反序列化的解码。

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

$unserialized_data = unserialize(base64_decode($data));

答案 1 :(得分:0)

那是因为您的'product_id'值包含[4]索引键'a:1:{i:4;...' 试试:

echo $rslt[4]['product_id'];