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条)

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?