开发者

Unserialize values from mySQL

开发者 https://www.devze.com 2023-02-14 18:25 出处:网络
I am using a classified scripts and saves user_meta data in the wp_usermeta table. The meta_key field is called user_address_info and in it there are all the data like below :

I am using a classified scripts and saves user_meta data in the wp_usermeta table. The meta_key field is called user_address_info and in it there are all the data like below :

s:204:"a:7:{s:9:"user_add1";s:10:"my address";s:9:"user_add2";N;s:9:"user_city";s:7:"my city";s:10:"user_state";s:8:"my phone";s:12:"user_country";N;s:15:开发者_运维知识库"user_postalcode";s:10:"comp phone";s:10:"user_phone";N;}";

I am not using all the fields on the script but user_add1, user_city, user_state and user_postalcode

I am having trouble to get the data using SQL like the example below (wordpress) :

$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_A);

I would like some help here so that I will display anywhere (I dont mind using any kind of SQL queries) the requested info e.g. the user_city of current author ID (e.g. 25)

I was given the following example but I want something dynamic

<?php
$s = 's:204:"a:7:{s:9:"user_add1";s:10:"my address";s:9:"user_add2";N;s:9:"user_city";s:7:"my city";s:10:"user_state";s:8:"my phone";s:12:"user_country";N;s:15:"user_postalcode";s:10:"comp phone";s:10:"user_phone";N;}"';
$u = unserialize($s);
$u2 = unserialize($u);
foreach ($u2 as $key => $value) {
   echo "<br />$key == $value";
}
?>

Thank you very much.


No, you can't use SQL to unserialize.
That's why storing serialized data in a database is a very bad idea

And twice as bad is doing serialize twice.

So, you've got nothing but use the code you've given.
I see not much static in it though.
do you experience any certain problem with it?
Or you just want to fix something but don't know what something to fix? Get rid of serialization then


i have found that the serialize value stored to database is converted to some other way format. Since the serialize data store quotes marks, semicolon, culry bracket, the mysql need to be save on its own, So it automatically putting "backslash()" that comes from gpc_magic_quotes (CMIIW). So if you store a serialize data and you wanted to used it, in the interface you should used html_entity_decode() to make sure you have the actual format read by PHP.

here was my sample:

$ser = $data->serialization; // assume it is the serialization data from database
$arr_ser = unserialize(html_entity_decode($ser));

nb : i've try it and it works and be sure avoid this type to be stored in tables (to risky). this way can solve the json format stored in table too.

0

精彩评论

暂无评论...
验证码 换一张
取 消