1

我的功能有问题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:{}";}}
4

2 回答 2

3

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

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

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

于 2012-12-06T19:36:03.480 回答
1

那是因为你的'product_id'值有一个[4]索引键'a:1:{i:4;...' 试试:

echo $rslt[4]['product_id'];
于 2012-12-06T19:29:42.563 回答