Unserialize values ​​from mySQL

I use classified scripts and save user_meta data in wp_usermeta table. The meta_key field is called user_address_info, and it has all the data, as shown 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 do not use all the fields in the script, but user_add1, user_city, user_state and user_postalcode

I'm having trouble getting data using SQL, as in the example below (wordpress):

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

I need help here, so I will show anywhere (I'm not against using any SQL queries) the requested information, for example. user_city of the 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";
}
?>

Many thanks.

+3
source share
2 answers

, SQL unserialize.

.

, , , .
. - ?
- , , ? ,

+5

, , , . , , culry, mysql , " " (), gpc_magic_quotes (CMIIW). , , html_entity_decode(), , , PHP.

:

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

nb: , , , ( ). json, .

0

Source: https://habr.com/ru/post/1796001/


All Articles