简体   繁体   English

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

[英]Retrieve serialize value in the database 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. 我正在尝试在wordpress中查询数据库中的序列化数组值,该值将存储在表wp_postmeta的meta_value列中。
Well, first I stored the array by using serialize() function of php. 好吧,首先我使用php的serialize()函数存储了数组。
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. 然后,当我尝试通过执行wordpress sql查询来检索它时。我期望它将是一个数组,因为它存储为数组,但是这样做之后,它显示为字符串而不是数组。

    $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 我确实使用is_array()函数检查了值,结果是它不是数组
Any idea on this to get the serialize value as an array? 关于将序列化值作为数组获取的任何想法吗?

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. s:30表示字符串长度30。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM