douluoxiao2286 2014-10-22 03:23
浏览 42
已采纳

检索数据库wordpress中的序列化值

I'm trying to query a serialized array value in the database in wordpress, value will be stored in the table wp_postmeta, in the column meta_value.
Well, first I stored the array by using serialize() function of php.
So for example,

$postID = 1;
$arr = array(1, 2, 3);
$ser_val = serialize($arr);
update_meta_data($postID, '_customvalue', $ser_val);

The stored values is something like this

s:30:"a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}";

Then when I tried to retrieve it by performing wordpress sql query.. What I was expecting that it will be an array since it is stored as array, but after doing so, it display as string not an array.

    $get_score = $wpdb->get_row("SELECT meta_value FROM wp_postmeta WHERE meta_key = '_cummulativescore'");
    $scr = unserialize($get_score->meta_value);
    var_dump($scr);

    //output displayed
    //string(30) "a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}" 

I did check the value using is_array() function, the result is that it is not an array
Any idea on this to get the serialize value as an array?

  • 写回答

1条回答 默认 最新

  • donglu9445 2014-10-22 04:51
    关注

    It looks like your data was converted to a string during serialization.

    The data should be stored as

    a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}
    

    instead of

    s:30:"a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
    

    s:30 means string length 30.

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

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制