dongqiuqiu4736 2018-05-12 06:57
浏览 295
已采纳

无法反序列化序列化字符串

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.

  • 写回答

2条回答 默认 最新

  • dongsha9208 2018-05-12 11:16
    关注

    This is because wrong serialize data is getting created from it.Even if we try to create JSON data, it is creating json data with wrong syntax.

    If we will try bellow codes, PDO will not allow it.

    1. $equipment['tables'] = isset( $_POST['equipment']['tables'] ) ? str_replace("'","", $_POST['equipment']['tables']) : '';

    2. $equipment['tables'] = isset( $_POST['equipment']['tables'] ) ? addslashes($_POST['equipment']['tables']) : '';

    3. $equipment['tables'] = isset( $_POST['equipment']['tables'] ) ? str_replace("'","\'", $_POST['equipment']['tables']) : '';

    results server error 500 in this case.

    and

    1. $equipment['tables'] = isset( $_POST['equipment']['tables'] ) ? addcslashes($_POST['equipment']['tables']) : ''; will results in NULL.

    We can can use either of 2 and 3 and change our PDO like this, that will solve the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图