PHP Unserilize is not working - php

I am saving an serialized array in mysql database .
and when i fetching result data from mysql and printing that details it is printing .
$data=$row['data'];
print_r( $data)
result is
a:8:{i:0;a:7:{s:2:"id";s:2:"16";s:4:"slug";s:8:"fieldset";s:4:"name";s:8:"Fieldset";s:4:"type";s:8:"fieldset";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:1;a:7:{s:2:"id";s:2:"20";s:4:"slug";s:9:"your-name";s:4:"name";s:9:"Your Name";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:11:"SamueljalIL";}i:2;a:7:{s:2:"id";s:2:"21";s:4:"slug";s:10:"your-email";s:4:"name";s:10:"Your Email";s:4:"type";s:5:"email";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:16:"dimitryg#msn.com";}i:3;a:7:{s:2:"id";s:2:"24";s:4:"slug";s:5:"phone";s:4:"name";s:5:"Phone";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:11:"88621295115";}i:4;a:7:{s:2:"id";s:2:"22";s:4:"slug";s:12:"your-message";s:4:"name";s:12:"Your Message";s:4:"type";s:8:"textarea";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:1876:"Havе yоu listenеd
http://boletines.consumer.es/?p=50&u=https://gdfgl/96D4u9";}i:5;a:7:{s:2:"id";s:2:"17";s:4:"slug";s:12:"verification";s:4:"name";s:12:"Verification";s:4:"type";s:12:"verification";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:6;a:7:{s:2:"id";s:2:"18";s:4:"slug";s:27:"please-enter-any-two-digits";s:4:"name";s:27:"Please enter any two digits";s:4:"type";s:6:"secret";s:7:"options";s:0:"";s:9:"parent_id";s:2:"17";s:5:"value";s:2:"82";}i:7;a:7:{s:2:"id";s:2:"19";s:4:"slug";s:6:"submit";s:4:"name";s:6:"Submit";s:4:"type";s:6:"submit";s:7:"options";s:0:"";s:9:"parent_id";s:2:"17";s:5:"value";s:0:"";}}
But when i try to unserilize this data it is not working
$arr=unserialize($data);
print_r($arr);
there is nothing printing here ;

Your serialized string has been damaged. As I rub my crystal ball, I can imagine someone manually performed string replacements (inappropriately) to update the url that immediately follows Have you listened in the array at index 4.
This is revealed after analyzing the data at this location:
s:1876:"Havе yоu listenеd
http://boletines.consumer.es/?p=50&u=https://gdfgl/96D4u9";
You see this stored value has 81 bytes/characters in it.
The serialized data strictly claims that the value must have 1876 bytes/characters in it.
Ultimately, your serialized data has been compromised -- either the length or the value.
If you are not bothered by the current value, you can manually repair the serialized data with this: https://3v4l.org/GqsHu
This is from a post of mine here: https://stackoverflow.com/a/55074706/2943403
With the provided snippet, you can either repair the corrupted serialized data on the fly each time, or you can take the time to repair all corrupted data and update your database so that this headache doesn't present itself again.
Let this occurrence be a lesson to developers -- Never try to take a short cut to update serialized data. You must unserialize it, modifying it, then re-serialize it so that a valid string is generated.

I think your serilized data truncate when storing into database. it might happen due to column length size. So increase you column length and try.

Related

Updating Serialized value in mysql

I want to update a Serialized value on meta_value column on mysql table.But json is hard to select from sql . How can i achieve.
a:2:{i:0;a:2:{s:19:"sevent_speaker_name";s:8:"John Doe";s:18:"sevent_speaker_img";a:1:{i:0;s:5:"72921";}}i:1;a:2:{s:19:"sevent_speaker_name";s:10:"John Smith";s:18:"sevent_speaker_img";a:1:{i:0;s:5:"72922";}}}
Here is the value. I want to replace all sevent with elevent but how can i do it?
Can I use LIKE? But first it need to be unserialized?.
This encoded information probably comes from WordPress or some other PHP framework's database; it's not JSON. Note that strings are encoded by storing the string length and the string contents:
s:19:"sevent_speaker_name"
You can use MySQL's REPLACE function to replace sevent with elevent, but you must be careful to update the length value s as well, or WordPress/PHP won't be able to read in the data.
It's possible to write a MySQL query to update the specific example given above, but it's difficult to write a query to substitutes all strings generically. Here is a tool that does the work.

Unserializing data

I have some order info stored in a DB table row called "order_details". From what research I've done so far, the cells contain a serialized array:
s:346:"{"total":50,"tax_perc":8.25,"shipping_total":20,"discount_total":0,"discount":"","pick_up":0,"items":[{"id":"0","amount":1,"available":1000,"price":50,"first_pet":50,"pet_count":1,"size":"4x6","type":"portrait","clothe":"93","total":50}],"size_found":false,"taxes_total":4.13,"grand_total":74.13}";
I tried using unserialize() but that didn't print anything. Leaving it serialized at least printed the contents of the cell.
Any ideas on why nothing is being printed when I use unserialize()?
Also, I'm trying to create a back-end interface using this data, so given that unserialize() will organize this data, I need to be able to show only a particular element (like ID). Is that possible? Or will it have to print all of that data?
UPDATE
Here's how what I have so far works:
(1) When someone places an order, the data is serialized and thrown into the db and an email is sent to both me and the client
(2) For this Admin page where I want to ask for and show said serialized data, I query the db to return the relevant table ordered by id
(3) Then I have this, which is meant to throw that data into a table row on the UI.
$mydata = $all_orders[$i]['order_details'];
$mydata = unserialize($mydata);
echo '<td>' . $mydata . '</td>';
At this point I am just trying to get a better visual on that data. I was also hoping to be able to maybe sort the table on price, ID, etc.
Thanks in advance for your responses!
The strings in your database have been JSON encoded to a string, then serialized. That's why the first character in the serialized string is an "s" (s means whatever follows is a string, in your case, of 346 characters in length).
The reason that unserialize() is returning false is because the string that has been serialized contains unescaped double quotes. When unserialize() runs, it fails as it uses the first 2 sets of double quotes to determine the string to unserialize. In your case, unserialize() is trying to unserialize the following string:
s:346:"{"
This causes a failure, as the string between the two sets of double quotes isn't 346 characters long.
You shouldn't need to serialize something that has already been JSON encoded. Choose one of the encoding options (JSON encoding or serializing) but not both, and stick with the one that you choose.

Getting data from unusualy formatted field in database

we have a field in a database that stores the data as below:
a:4:{
s:10:"subscriber";
b:1;s:32:"access_s2member_ccap_one";
b:1;s:32:"access_s2member_ccap_two";
b:1;s:32:"access_s2member_ccap_three";
b:1;}
We need to grab all the ccaps that are in this field but don't know the best way to do it, we can return the above as a string from the field in the database but don't know what to do with it next. The ccaps will always start with access_s2member_ccap but the s:32 in front of it will change each time. We need to get all of them and in some cases there may be as little as one as well as more than three.
Any ideas?
You have to unserialize your data.

how to save json string in a mysql field

I have a table with a text field. I received string as below:
"value": "{\"airwayBills\":[\"84498761\"],\"dockets\":[\"1652395\"],\"error\":false,\"muditaError\":[{\"actionType\":\"docket\",\"docketNo\":\"1652395\",\"errorMessage\":\"No thing error\",\"pieceList\":null,\"quantity\":\"1\"}],\"muditaManifestExtra\":\"Unknown\",\"thingDetails\":[{\"airline\":\"Unknown\",\"bookingDate\":\"19-05-2014 13:03:15+0530\",\"currentPiecesDelivered\":\"0\",\"currentPiecesManifested\":\"1\",\"destination\":\"GOI\",\"flight\":\"Unknown\",\"inScannedPieces\":[],\"locationId\":\"MUDDELHUB1\",\"mode\":\"Air\",\"number\":\"1652395\",\"ordinality\":\"0\",\"origin\":\"DEL\",\"outScannedPieces\":[],\"quantity\":\"1\",\"recipientEmailId\":\"Unknown\",\"serviceProvider\":\"Unknown\",\"shipper\":\"Vector E Commerce Pvt. Ltd.\",\"thingType\":\"docket\",\"totPiecesManifested\":\"1\",\"totalPiecesDelivered\":\"0\",\"weight\":\"11\"}]}",
This value get stored correctly inside a field in the table, but when I try to fetch this value and save it in another field of another table then some of the values like "airwayBills\":[\"84498761\"] are stored as "airwayBills\":["0"], this is causing the loss of data. There is one more strange behaviour that this is happening randomly with only some of the records. Some records are added correctly. For saving into another field I am just enclosing the string in a single quote to save the string as is.
Please help how to save it in a single field in Mysql. Please note that the above values are coming after a GROUP_CONCAT query on the first table.
Thanks in advance
Consider the variable name $json_string as the one that contains your json in the give state (in your post), try stripslashes before trying to insert it into the database.
Here's an extra question, are you building the json array manually using string concatenation or using php array and them json_encode ? (2nd one would make things easier for you, since when you need to add the json obtained after the first mentioned query ran you can just do json_encode and your done)

How do obfuscate JSON response when dealing with a DB query?

[
{
"businesscards_id":"12",
"X_SIZE":"1.75x3",
"X_PAPER":"14ptGlossCoatedCoverwithUV(C2S)",
"X_COLOR":"1002",
"X_QTY":"250",
"O_RC":"NO",
"F_PRICE":"12490",
"UPS_GROUND":"12000",
"UPS_TWODAY":"24000",
"UPS_OVERNIGHT":"36000"
}
]
This JSON encode response is seen in console of Chrome. This array is being returned from a DB query. It is showing my table column names. For security reasons I do not want to show my table column names. How can this JSON object be obfuscated or hashed and/or encoded or dynamically re-written to keep my table col names private?
Don't do anything to your JSON.
If you don't want your column names to be visible, just dont use your column names. Create a new array using new keys to send with JSON and then change that array back into one containing your column names afterwards.
But it really shouldn't be a problem people seeing them. Nobody has access to your database so letting people see column names isn't an issue.
It really depends on how you wish to use the record once it has been received. One strategy might be to return an array of the values only, discarding the keys. Then in your code, use your private knowledge of which array value you need when you process the record. Something like:
var result=[];
Object.keys(record).forEach(function(key){result.push(record[key]);});
And then in your code, use array indices to access the values.
SQL statement:
SELECT `col_name` AS 'something_else'
But also, as everyone else said, don't do this for security. It is pointless.

Categories