Read this using php - php

Is there any way to read/write the following data using php? And what is this called?
a:1:{s:13:"administrator";s:1:"1";}

Looks like data serialized by the PHP serialize() function. You can read it using unserialize().
$serialized = 'a:1:{s:13:"administrator";s:1:"1";}';
$data = unserialize($serialized);
print_r($data);

try using unserialize() if that is a serialized data then that should unserialize it.

Yep... That's serialized data. You can unserialize it for pure chechup purposes here - http://unserialize.net/serialize.
Other than that use unserialize() in your PHP code.

Related

Is this Json Data, XML or Arrays?

I am confused on what kind of data is this. Can anyone help me on this. I am trying to make it in readable format using PHP.
a:6:{s:12:"cfdb7_status";s:6:"unread";s:9:"your-name";s:6:"Sujith";s:10:"your-email";s:21:"sudhamenon990#gmail.com";s:12:"your-subject";s:5:"Hello";s:12:"your-message";s:16:"How are you man?";s:16:"ywctm-product-id";s:4:"1840";}
Thank you in advance.
It is a serialized array. You need to unserialize it
unserialize ( string $data , array $options = [] )
unserialize() takes a single serialized variable and converts it back into a PHP value.So that you can access it using php.

Save and retrieve a php array through mysql

I want to save a long array in mysql database and when i read that array back from mysql database, i want that array back in proper array format. Is this possible?
Other threads are suggesting to convert array into string using serialise and explode. But will they help me to get proper array back?
Thanks.
You can try it yourself.
As an alternative to serailize(), you can use json_encode().
sidenote: reverse of serialize() is unserialize(), not explode().
sidenote: reverse of json_encode() is json_decode().
sidenote: Very worthwhile to read: Discussion of json_encode() vs serialize()
Usually, serialize and unserialize functions are used for such purposes.
With serialize you can convert your array to a string and than get an array by applying unserialize on string you get from serialize.
And about explode: you may use it too, but than you will need to use implode function to serialize array. But it will work with simplest one dimensional arrays only:
implode(",", array("val1", "val2", "val3")) = "val1,val2,val3"

Coverting String to PHP Associative Array

How can i convert this string
$str = "array('3'=>'wwm','1'=>'wom')";
to real php associative array...
It's simple but REALLY INSECURE
$str = "array('3'=>'www.tension.com','1'=>'www.seomeuo.com','requestedBy'=>'1')";
eval("\$array = $str;");
You never should use this approach, there another ways to do it like: serialize() and unserialize()
You can use the eval() function for that:
$str = "array('3'=>'wwm','1'=>'wom')";
eval("\$a=$str;");
var_dump($a);
However using eval() in your code is considered to be risky and you should not use it. Try to use serialize(), unserialize() instead.
First of all. Do not use eval. It is Evil!
http://af-design.com/blog/2010/10/20/phps-eval-is-evil/
Secondly. The simple solution would not to be using this string but simply to use "serialize" when you put it in the DB and unserialize when you pull it out. You are storing a very unusual format.

convert POST array to json format

How can I convert php's POST variable to json format?
If this is entirely in php, and you simply want to convert the data in $_POST to JSON, you can do so like this:
$json = json_encode($_POST);
Using php's built-in function json_encode (doc)
If this is not what you want to do, please be more specific with your question.
json_encode($_POST);
Docs: http://php.net/manual/en/function.json-encode.php

Data stored in json

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.

Categories