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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀