Reputation: 127
I am storing data in mysql table by serialize method, now I want to print all data So I wrote mysql query and trying to unserialize because data is in serialize format but unserialize showing error.
Error:
unserialize() expects parameter 1 to be string, array given
Query to fetch all records
$this->db->select('*');
$this->db->from($table);
$result=$this->db->get()->result();
$unserialize_list=unserialize($result);
Upvotes: 0
Views: 5351
Reputation: 1301
Your $result
variable contains a multi-dimensional array.
Assuming that some of the data in the table is serialized, and since you have not posted your table schema, here is a sample table that I hope matches your use case:
Id | Data
-------------------
1 | a:2:{s:4:"Name";s:5:"Test1";s:8:"Location";s:9:"Somewhere";}
2 | a:2:{s:4:"Name";s:5:"Test2";s:8:"Location";s:14:"Somewhere Else";}
3 | a:2:{s:4:"Name";s:5:"Test3";s:8:"Location";s:18:"Somewhere Else Too";}
Running this code:
$this->db->select('*');
$this->db->from($table);
$result=$this->db->get()->result();
$unserialize_list=unserialize($result);
Will result in an array of objects, one for each line in your table, like this:
Array
(
[0] => stdClass Object
(
[Id] => 1
[Data] => a:2:{s:4:"Name";s:5:"Test1";s:8:"Location";s:9:"Somewhere";}
)
[1] => stdClass Object
(
[Id] => 2
[Data] => a:2:{s:4:"Name";s:5:"Test2";s:8:"Location";s:14:"Somewhere Else";}
)
[2] => stdClass Object
(
[Id] => 2
[Data] => a:2:{s:4:"Name";s:5:"Test3";s:8:"Location";s:18:"Somewhere Else Too";}
)
)
You need to run the following code in order to access the unserialized data:
foreach ($result as $line) {
$unserializedData = unserialize($line->Data);
// Use the unserialized data as needed...
print_r($unserializedData);
}
Upvotes: 3