dongou3158 2016-07-26 06:14
浏览 124
已采纳

从数据库反序列化序列化字符串时出现错误消息的原因?

Well, I have following array and variables and it's output is bellow :

`print_r($menu_ex_qnt);`

Array
(
    [0] => 1
    [1] => 2
)


print_r($menu_extra_item_name);

Array
(
    [0] => 238
    [1] => 239
)

$final_ex = array_combine($menu_ex_name, $menu_ex_qnt);        
print_r($final_ex);

Array
(
    [238] => 1
    [239] => 2
)

echo $final_ex = serialize($final_ex);

a:2:{i:238;s:1:"1";i:239;s:1:"2";}

Now, I am inserting this $final_ex variable to database.

But when I get this variable from database and try to unserialize it then it's showing me following error message :

echo unserialize($p_ext_items);
Note : This $p_ext_items contain the serialize string e.g: a:2:{i:238;s:1:"3";i:239;s:1:"1";}

Notice: unserialize(): Error at offset 11 of 54 bytes .......

why ?

What I actually want

There are 2 variables $menu_ex_qnt and $menu_extra_item_name. I want to save these 2 variables value not key to the database. So that, in final variable there will be key and value pair.

For e.g:

238 => 1
239 => 2
  • 写回答

1条回答 默认 最新

  • dskyx46424 2016-07-26 06:31
    关注

    Since you said that $p_ext_items contains the serialize string which is :- a:2:{i:238;s:1:"1";i:239;s:1:"2";}

    and you did:-

    $p_ext_items = htmlspecialchars($result['p_ext_items']);
    echo unserialize($p_ext_items);
    

    You have to remove first line, not needed in this case, so simply you need to do:-

    print_r(unserialize($p_ext_items)); // it will give array so use print_r
    

    Note:- Working example for you:- https://eval.in/611959

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)