Data stored in json - php

I have my data stored in a JSON string like these...
a:1:{s:15:"s2member_level1";s:1:"1";}
How can i read this values in mysql?
I need to know if the value "s2member_level1" is 1.
Thanks!!!

That's not JSON but a string resulted from calling serialize() in PHP. You cannot parse it easily in MySQL. If you can use PHP, use the unserialize function:
$obj = unserialize($data_from_mysql);
if ($obj['s2member_level1'] == 1) {
// more code here
}
You can convert data to JSON in PHP using the json_encode function. In a similar way, you construct an object from a JSON string using json_decode.

#Lekensteyn is correct, but you could do a like statement, although its performance would most likely be very poor. My true answer is to change how you store this information to take advantages of best performing queries.
select * from table
where column like '%s:15:"s2member_level1";s:1:"1";%';

#Lekensteyn is right about the type of this particular String, but for others, PHP has json_decode which you can use to convert a JSON object to a PHP object. It would be considerably more difficult to read such an object using MySQL only.

This is no json, but serialized data. It was probably serialized with the 'serialize' function of PHP. Try:
print_r(unserialize('a:1:{s:15:"s2member_level1";s:1:"1";}'));
... to unserialize it.

Related

Get information from a JSON object that is stored as a string in the database using php [duplicate]

Hi I have a api call that returns a string like the following, and I need to convert it in a JSON object to process.
"a:1:{s:19:\"is_featured_service\";b:0;}"
That's a serialize()d string. unserialize() it, then json_encode() it:
<?php
$string = "a:1:{s:19:\"is_featured_service\";b:0;}";
$json = json_encode(unserialize($string));
var_dump($json);
Be careful, though. Per PHP manual:
Warning Do not pass untrusted user input to unserialize() regardless
of the options value of allowed_classes. Unserialization can result in
code being loaded and executed due to object instantiation and
autoloading, and a malicious user may be able to exploit this. Use a
safe, standard data interchange format such as JSON (via json_decode()
and json_encode()) if you need to pass serialized data to the user.
Demo
serialize() reference
unserialize() reference

PHP and JSON datatype in MySQL

Despite of recent implementation of JSON datatype to MySQL I can't find any word on it in the related PHP documentation.
My question is: will PHP automatically convert cells of JSON column to the actual values - arrays or literals - or will it provide just json-encoded strings. Like:
$sql_query = "SELECT JSON_ARRAY(1,2,3)";
$result = mysqli_query($sql_query);
$value = mysqli_fetch_row($result)[0];
// what is a $value? Array(1,2,3) or a string "[1,2,3]"
// do I have to use json_decode() to get an actual array here?
(Don't have MySQL 5.7 at hand right now, so can't check it myself.)
i think with serialize and unserialize you get whatever you want.
so just store all values using serialize and you can get that values with unserialize.
so just check serialize and unserialize.

Can PHP easily parse JSON objects into PHP objects?

More or less, my question is as above.
I have A lot of data i am going to serialize and send to the server. With that said, is there a PHP function to parse it into PHP-objects to manipulate on the Serverside?
My thought is yes due to the dynamic nature of PHP, but i wasnt sure what it would be.
You essentially answered your own question. From the PHP manual entry for json_decode:
json_decode
Takes a JSON encoded string and converts it into a PHP variable.
That said, you'll obviously want to do all the requisite error checking and such.
You can also use json_decode with the true parameter to effectively convert it into to a PHP array like so: $var = json_decode($object,true);

PHP mysql code help decoding

I have this value under Items in my DB:
a:1:{i:0;a:9:{s:12:"model_number";s:10:"TT1-W";s:5:"price";s:4:"3810";s:10:"unit_price";d:3135.6300000000001091393642127513885498046875;s:8:"id_price";d:3810;s:9:"sales_tax";d:290.3700000000000045474735088646411895751953125;s:5:"sales";d:3084.6300000000001091393642127513885498046875;s:7:"service";s:2:"51";s:7:"freight";s:3:"384";s:13:"co_cat";s:3:"X4";}}
Making it more reader-friendly:
a:1:
{
i:0;
a:9:
{
s:12:"model_number";
s:10:"TT1-W";
s:5:"price";
s:4:"3810";
s:10:"unit_price";
d:3135.6300000000001091393642127513885498046875;
s:8:"id_price";
d:3810;
s:9:"sales_tax";
d:290.3700000000000045474735088646411895751953125;
s:5:"sales";
d:3084.6300000000001091393642127513885498046875;
s:7:"service";
s:2:"51";
s:7:"freight";
s:3:"384";
s:13:"co_cat";
s:3:"X4";
}
}
I am unable to find out how to decode this string since it can not seem to find reference to it in the php code that displays it on the page. It looks to me to be JSON but i can not seem to find a "standard" format for the above in order to start figuring out where it starts and where it ends.
I am needing this to be decoding using ASP.net. But then again, i need to figure out what it is before i can start decoding it!
Any help to what it is would be great!
Try with unserialize:
function.unserialize
EDIT: If you can use C# libraries:
How to unserialize PHP Serialized array/variable/class and return suitable object in C#
EDIT2:
Visual Studio Tip: Three ways to use C# within a VB.NET project
EDIT3:
i need to figure out what it is
It's standard PHP-solution to store (and restore) arrays and objects (and other types, see manual) in strings.
That appears to be PHP's serialization methodology. You just need to use PHP's unserialize() on it.
This looks a serialized object. PHP's unserialize is probably what you want:
unserialize() takes a single serialized variable and converts it back
into a PHP value.
There is no built in way to turn that into an ASP.Net object, but it is a regular format, so you can build your own parser to create a simple dictionary representation of the attributes of that particular structure.
But if you're trying to de-serialize a PHP object in ASP.Net you're probably doing something wrong!

What datatype is this? is it wordpress specific?

Hey can anyone tell me what datatype this is? it wont parse as i've stripped out sensitive data. Am I correct in thinking its json?
a:7:{s:12:"competitions";a:893:{i:1;s:10:"Ersie";i:5;s:19:"General
News"1510126584;s:0:"";i:1019;s:0:"";i:8284;s:0:"";i:191016;s:0:"";i:284;s:0:"";i:91019;s:0:"";i:81863;s:0:"";i:1563;s:0:"";i:710138;s:0:"";i:101333;s:0:"";i:33430;s:0:"";i:10224;s:0:"";i:10430;s:0:"";i:13430;s:0:"";i:375;s:0:"";i:72107;s:0:"";i:11372;s:0:"";i:181372;s:0:"";i:1885;s:0:"";i:107155;s:0:"";i:10284;s:0:"";i:8206;s:0:"";i:8101316;s:0:"";i:1913;s:0:"";i:206;s:0:"";i:772138;s:0:"";i:72284;s:0:"";i:1672155;s:0:"";i:7101663;s:0:"";i:891013;s:0:"";i:101843;s:0:"";i:1107;s:0:"";i:1072;s:0:"";i:830;s:0:"";i:7284;s:0:"";i:8101333;s:0:"";i:13372;s:0:"";i:1570;s:0:"";i:2543;s:0:"";i:91316;s:0:"";i:385;s:0:"";i:8385;s:0:"";i:2843;s:0:"";i:695;s:0:"";i:1970;s:0:"";i:1661;s:0:"";i:18206;s:0:"";i:33155;s:0:"";i:787;s:0:"";i:8117;s:0:"";i:1943;s:0:"";i:3043;s:0:"";i:872239;s:0:"";i:872155;s:0:"";i:910104;s:0:"";i:8125;s:0:"";i:239;s:0:"";i:8428;s:0:"";i:1382;s:0:"";i:87293;s:0:"";i:95385;s:0:"";i:11672;s:0:"";i:92572;s:0:"";i:828;s:0:"";i:8239;s:0:"";i:640;s:0:"";i:87155241;s:0:"";i:26155241;s:0:"";i:87158241;s:0:"";i:69158241;s:0:"";i:82543;s:0:"";i:193372;s:0:"";i:90163241;s:0:"";i:163372;s:0:"";i:1016107;s:0:"";i:86372;s:0:"";i:87163241;s:0:"";i:155162241;s:0:"";i:713121;s:0:"";i:2690241;s:0:"";i:895430;s:0:"";i:-403549467;s:0:"";i:2060490689;s:0:"";i:7181924;s:0:"";i:157158241;s:0:"";i:87295;s:0:"";i:71672430;s:0:"";i:1372430;s:0:"";i:9430;s:0:"";i:897;s:0:"";i:242;s:0:"";i:87162241;s:0:"";i:161863;s:0:"";i:1824107;s:0:"";i:26165241;s:0:"";i:7891316;s:0:"";i:81670;s:0:"";i:43107;s:0:"";i:710121;s:0:"";i:172283;s:0:"";i:8790241;s:0:"";i:253372;s:0:"";i:71863;s:0:"";i:26157241;s:0:"";i:8393;s:0:"";i:91824;s:0:"";i:826;s:0:"";i:63640;s:0:"";i:26163241;s:0:"";i:13121;s:0:"";i:82574;s:0:"";i:165241;s:0:"";i:87157241;s:0:"";i:2687241;s:0:"";i:26162241;s:0:"";i:199;s:0:"";i:18910;s:0:"";i:162165241;s:0:"";i:751430;s:0:"";i:16125;s:0:"";i:81893;s:0:"";i:79101316;s:0:"";i:81693;s:0:"";i:8913119;s:0:"";i:7818;s:0:"";i:72223;s:0:"";i:781072;s:0:"";i:972107;s:0:"";i:26241;s:0:"";i:90158241;s:0:"";i:87071;s:0:"";i:816125;s:0:"";i:72155430;s:0:"";i:72109;s:0:"";i:8106;s:0:"";i:181;s:0:"";i:8126;s:0:"";i:897293;s:0:"";i:187;s:0:"";i:101318;s:0:"";i:7166372;s:0:"";i:13216;s:0:"";i:101367;s:0:"";i:91149;s:0:"";i:781633;s:0:"";i:7101363;s:0:"";i:872430;s:0:"";i:1657;s:0:"";i:215;s:0:"";i:1025;s:0:"";i:7136372;s:0:"";i:1314;s:0:"";i:101319;s:0:"";i:1372344;s:0:"";i:1372573;s:0:"";i:1672430;s:0:"";i:872109;s:0:"";i:972430;s:0:"";i:193372430;s:0:"";i:13171;s:0:"";i:172430;s:0:"";i:6372430;s:0:"";i:772430;s:0:"";i:1372155430;s:0:"";i:71372;s:0:"";i:887;s:0:"";i:78933344;s:0:"";i:8131672;s:0:"";i:131672;s:0:"";i:365;s:0:"";i:91172;s:0:"";i:772155;s:0:"";i:972155;s:0:"";i:137072;s:0:"";i:729;s:0:"";i:781672;s:0:"";i:891172;s:0:"";i:924;s:0:"";i:796372;s:0:"";i:94372;s:0:"";i:284385;s:0:"";i:72344;s:0:"";i:71372121;s:0:"";i:78972;s:0:"";i:136372;s:0:"";i:713211;s:0:"";i:17972;s:0:"";i:101872;s:0:"";i:17872;s:0:"";i:84370;s:0:"";i:71633;s:0:"";i:172107;s:0:"";i:833732;s:0:"";i:18101318;s:0:"";i:816155;s:0:"";i:83343;s:0:"";i:43155;s:0:"";i:438;s:0:"";i:104363;s:0:"";i:961;s:0:"";i:843732;s:0:"";i:78107;s:0:"";i:13344;s:0:"";i:72393;s:0:"";i:83363;s:0:"";i:113121;s:0:"";i:863121;s:0:"";i:8131643;s:0:"";i:518;s:0:"";i:1789;s:0:"";i:61107;s:0:"";i:891619;s:0:"";i:43214;s:0:"";i:63121;s:0:"";i:7136430;s:0:"";i:81865;s:0:"";i:728430;s:0:"";s:11:"10245074125";s:0:"";i:63162;s:0:"";i:393;s:0:"";i:89732;s:0:"";i:2633430;s:0:"";i:157283430;s:0:"";}s:8:"lastpost";i:1281041491;s:7:"default";s:19:"General
News";s:11:"autopublish";s:2:"on";s:5:"draft";N;s:6:"poster";s:1:"1";s:6:"suffix";s:0:"";}
This is a serialized PHP array. Essentially, it's just a string that represents a PHP array (the a at the beginning marks it as an array).
You can get the PHP array back out of it by passing the string to the PHP function unserialize().
That is serialized data - not Wordpress specific.
Take a look into serialize(), unserialize() and OOP serialization: __sleep()/__wakeUp().

Categories