如何从MySQL反序列化值并使用PHP / CodeIgniter在变量中赋值?

时间:2013-08-28 02:23:58

标签: php mysql codeigniter

大家好,我只需要一些关于反序列化MySQL值的帮助。我想要的是从我的数据库中获取序列化值后,我将在变量中分配未序列化的值。

这是我的代码

//THIS IS THE QUERY

$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_order_data.data as data_serialize FROM cscart_orders
                           LEFT JOIN cscart_order_data
                           ON cscart_orders.order_id = cscart_order_data.order_id
                           WHERE type = 'I'";

$resultGetSerialize = $this->db->query($sqlGetSerializedValues);

$var_data = array();

foreach($resultGetSerialize->result_array() as $row1){
   $var_data[] = $row1['data_serialize'];
}
unserialize($var_data); //How can i get the selected value
print_r($var_data);

OUTPUT:

Array
(
    [0] => a:2:{s:6:"points";i:100;s:4:"cost";i:100;}
    [1] => a:2:{s:6:"points";i:100;s:4:"cost";i:100;}
    [2] => a:2:{s:6:"points";i:294;s:4:"cost";i:294;}
    [3] => a:2:{s:6:"points";d:107;s:4:"cost";d:107;}
    [4] => a:2:{s:6:"points";i:163;s:4:"cost";i:163;}
    [5] => a:2:{s:6:"points";i:322;s:4:"cost";i:322;}
    [6] => a:2:{s:6:"points";i:289;s:4:"cost";i:289;}
    [7] => a:2:{s:6:"points";i:9;s:4:"cost";i:9;}
    [8] => a:2:{s:6:"points";i:500;s:4:"cost";i:500;}
    [9] => a:2:{s:6:"points";i:500;s:4:"cost";i:500;}
    [10] => a:2:{s:6:"points";i:301;s:4:"cost";i:301;}
    [11] => a:2:{s:6:"points";i:500;s:4:"cost";i:500;}
    [12] => a:2:{s:6:"points";i:490;s:4:"cost";i:490;}
    [13] => a:2:{s:6:"points";i:103;s:4:"cost";i:103;}

例如,我想获得“积分”并在变量$ points中分配。 我怎样才能做到这一点?还有成本。好的,这都是提前谢谢。

2 个答案:

答案 0 :(得分:2)

在你的循环中,你想在那里调用unserialize函数:

foreach($resultGetSerialize->result_array() as $row1){
   $var_data[] = unserialize($row1['data_serialize']);
}

然后你将结束一个多维数组。现在你正在清理一系列序列化值。

答案 1 :(得分:1)

据我所知,提取这些值(由PHP而不是MYSQL为serialize() d)的唯一方法是使用PHP的unserialize()函数(docs here)