Miko
Miko

Reputation: 2633

unserialize problem

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?

Upvotes: 0

Views: 465

Answers (4)

Bhupesh
Bhupesh

Reputation: 881

$records = array(
            'name'=>'abc', 
            'mobile'=>'1234566789', 
            'address'=>'test', 
            'email'=>'[email protected]');
$records_serialize = serialize($records);

echo "serialize<br/>";
print_r($records_serialize);

echo "<br/><br/>unserialize<br/>";
$records_unserialize = unserialize($records_serialize);
print_r($records_unserialize);

Here code to use serialize and unserialize

output

serialize
a:4:{s:4:"name";s:3:"abc";s:6:"mobile";s:13:"1234566789";s:7:"address";s:4:"test";s:5:"email";s:13:"[email protected]";}

unserialize
Array ( [name] => abc [mobile] => 1234566789[address] => test [email] => [email protected] )

Upvotes: 1

Patrick Hankinson
Patrick Hankinson

Reputation: 283

You need to use the unserialize function. This will return every back into an array.

Upvotes: 0

Aron Rotteveel
Aron Rotteveel

Reputation: 83173

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'])); 

Upvotes: 3

Related Questions