Getting data from unusualy formatted field in database - php

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.

Related

PHP Unserilize is not working

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.

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)

CakePHP, model->save() after model->read(), field "modified" not automagically set though not present in data

in a CakePHP 2.3 project, in one of the controller actions I want to update several records of a table. The data to be updated is posted as an array, and I iterate through that array.
However, some new field values are related to current field values, therefore I cannot simply write the data in an array $data and do model-save($data). Instead I do
$record = model->read(null, $id); //$id is retrieved from the posted data array.
$record['some_field'] = $new_value;
unset($record['modified']);
//in addition I used model->modified = null;, but to no avail
model->save($record);
Problem is, that the field modified is not automagically updated. In the CakePHP documentation I found that the value for "Modified" must not be present in the data that is to be saved. But the unset() alone doesn't seem to be enough.
In cakePHP - modified field not updating user tadasZ mentioned that it doesn't work when you use model->read() in advance.
I couldn't find anything about it in the documentation. But if that is the case, is there any way at all to use the Automagic for the field modified? I can set the field value myswlf (in fact, right now that's what I do as a workaround), but if there is an automatic way, I would like to use it.
When you are using Model::read(), the result is still in the same CakePHP format of $array['Model']['field'] so you would have to do unset($record['Model']['modified']);
The answer is here:
http://book.cakephp.org/2.0/en/models/saving-your-data.html#using-created-and-modified
If you have created or modified data in your $this->data (e.g. from a Model::read or Model::set) before a Model::save() then the values will be taken from $this->data and not automagically updated. If you don’t want that you can use unset($this->data['Model']['modified']), etc.

What is the best way to expand existing data in a database with PHP

So I've got a field in my database that will contain serial id numbers separated by commas eg. 2817,2385,4937,3298 I want to be able to add more numbers to the same field over time.
The best way I can think to do this is to get the contents, add the new numbers to it, and insert them back into the database.
What I'm wondering is if there's a more direct way. I had trouble thinking of a good way to word this that yields helpful search results so I'm asking here.
Yes there is.
UPDATE `table` SET `column` = CONCAT(`column`,',new_serial')
However this is not right, you should never store comma separated values. It's called database normalization.
Try this:
UPDATE `tableName` SET `yourColum` = CONCAT(`yourColumn`, ',nextId')
This will update your column as you requested.

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