I have an serialized string and I am trying to unserialized it, but getting false.
Serialized String echoed:
string(175) "a:6:{s:6:"tables";s:9:"8 Tables";s:8:"table_no";s:1:"6";s:6:"chairs";s:6:"Chairs";s:8:"chair_no";s:1:"6";s:12:"round_tables";s:11:"Round Table";s:14:"round_table_no";s:1:"6";}"
Data in database:
a:6:{s:6:"tables";s:9:"8 Tables";s:8:"table_no";s:1:"6";s:6:"chairs";s:6:"Chairs";s:8:"chair_no";s:1:"6";s:12:"round_tables";s:11:"Round Table";s:14:"round_table_no";s:1:"6";}
code to unserialize the data
var_dump(unserialize($fellowship_data['equipment']));
Data coming in post request before serializing it and saving to database
["equipment"]=>
array(7) {
["tables"]=>
string(9) "8' Tables"
["table_no"]=>
string(1) "6"
["chairs"]=>
string(6) "Chairs"
["chair_no"]=>
string(1) "6"
["round_tables"]=>
string(11) "Round Table"
["round_table_no"]=>
string(1) "6"
["piping_drapes"]=>
string(13) "Piping Drapes"
}
Code before making it serialize
$equipment = array();
if ( isset($_POST['equipment']) ){
$equipment['tables'] = isset( $_POST['equipment']['tables'] ) ? str_replace("'","", $_POST['equipment']['tables']) : '';
$equipment['table_no'] = isset( $_POST['equipment']['table_no'] ) ? $_POST['equipment']['table_no'] : '';
$equipment['chairs'] = isset( $_POST['equipment']['chairs'] ) ? $_POST['equipment']['chairs'] : '';
$equipment['chair_no'] = isset( $_POST['equipment']['chair_no'] ) ? $_POST['equipment']['chair_no'] : '';
$equipment['round_tables'] = isset( $_POST['equipment']['round_tables'] ) ? $_POST['equipment']['round_tables'] : '';
$equipment['round_table_no'] = isset( $_POST['equipment']['round_table_no'] ) ? $_POST['equipment']['round_table_no'] : '';
}
Code to serialize the data
$equipment = serialize($equipment);
Unserializing it returning false
.
Update I changed the code and used json_encode and json_decode and stored in databsae as text, now while json decoding I am getting NULL!!
Can any one give me some idea how it could be solved?
If needed more information, please ask me question, so that I will add those to question.
Note: The server I am working on does not support json
data type in its database, so I was bound to use serialize. I do not recommend people to follow it and use serialize()
to save it.