in my mysql cell this is stored using serialize function
a:1:{i:0;s:275:"a:4:{s:8:"khghg_id";s:10:"foo1187";s:3:"uri";s:21:"foo/vtory/1187";s:4:"name";s:5:"nmart";s:5:"tuhlmb";a:3:{i:0;s:40:"knuujhs/201205/13_03_pceb9.jpg";i:1;s:40:"knuujhs/201205/13_03_0wlih.jpg";i:2;s:40:"knuujhs/201205/13_03_tq5wf.jpg";}}";}
i am trying to do unserialize
i am using this code
$cell =$row9['attachment'];
$list = unserialize($cell);
$info = unserialize($list[0]);
var_dump($info);
when i am trying with this i am getting error bool(false) error
so i tried with parse_str
with parse_str i did not get any error
parse_str($cell,$list );
but i am not getting the output in my database
i am storing the output in database and i am submitting the query to database .everything is getting stored other than this unserialize values .here u can note that there are
khghg_id which is foo1187
uri which is foo/vtory/1187
name which is nmart
i want to store these details in my database so i am using
'.$info['khghg_id'].' for sending the data to mysql but mysql stores everything other than all unsterilized values
You retrieve a serialized text from database.
You unserialize it.. change it and then when you save it back to database you need to serialize it back
//retrieve serialized data from database
$arr = unserialize($row['properties']);
//get the desired value
$khghg_id = $arr['khghg_id'];
//now insert $khghg_id in database
From the code you posted above.. I see you are unserializing 2 times.. you dont really need to do this
$cell =$row9['attachment'];
$list = unserialize($cell);
$info = $list[0]; //this line should make the difference
var_dump($info);
Related
i want to update my column data in a way that posted data gets appended at the end of the existing column data. Current data is in Json format
This is how i am updating record
$data=array('services', $array );
$this->db->where('id',$id)
$this->db->update('garage',$data);
but this updates whole record how do i append the record at the end of json
You can select the data from the database and use PHP way to concatenate the string and run the update.
$d=$this->db->get_where('garage',array('id',$id))->row();
Now merge the you can merge the existing data with new data.
$new_data=$d->services.json_encode($array);
$data=array('services', $new_data );
$this->db->where('id',$id)
$this->db->update('garage',$data);
Hope this will fix the issue.
I have JSON data that is stored in a variable which is a set of permissions for a particular user. The JSON data however, is not stored in a file, it's just in a database column.
I've tried to look for a method of updating a permission (which is either true or false) but I've had no luck thus far. At the moment, I have something to the effect of;
The raw JSON data...
{
"permissions": {
"permission_1": true,
"permission_2": false
}
}
Getting it out of the database...
$permissions = json_decode($data, true);
How do I (using PHP) update the JSON data to say, update permission 2 to true? I'm using json_decode() when it comes out of the database and putting it into an array but that's only for using it. After that I'm lost as to how to update the value.
In order:
Extract column data from the database.
"SELECT column FROM table WHERE id = 1 LIMIT 1"
JSON_decode data into a set of PHP values/objects/arrays/whatever.
$array = json_decode($OutputRow['column'],true);
Update the value(s) you need to update.
$array['permissions']['permission2'] = true;
Recompile into a JSON string using JSON_encode.
$data = json_encode($array);
Update the database (SQL?) with the new JSON_encoded string.
"UPDATE table SET column = :data WHERE id = 1 LIMIT 1"
My examples use PDO and MySQL by reference but does not go into any details about database interaction as you've not given any details as to how you're reaching your database. It's only a ballpark rough example of how to do the points listed.
I have a database where I store some data, and one field is a json string.
I insert an json in this field:
$stmt->bindParam(":settings", json_encode($widget->settings));
Then when I try to retrieve the record I get my row with the settings column as string. I need my data to be json, so I should decode this field before output my records. If i go with:
$app->response->setBody(json_encode($data, JSON_NUMERIC_CHECK));
I get something like:
"name":"My Name","label":null,"row":null,"settings":"{\"site\":\"dfsdf\",\"action\":\"UrlInfo\"}"
with settings escaped. I should first decode settings and then encode again to output my results. How can I do to solve this?
UPDATE:
I retrieve my data using PDO, so I get an array:
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
Before I save this I have:
"settings":{"site":"fff","action":"UrlInfo"}}
When you retrieve the data, you should use json_decode to reverse the encoding that you did when you inserted it.
foreach ($data as &$row) { // Use reference so we can modify in place
$row['settings'] = json_decode($row['settings']);
}
$app->response->setBody(json_encode($data, JSON_NUMERIC_CHECK));
I have this serialize data in my mySQL database
a:4:{i:0;s:7:"bvl.png";i:1;s:8:"ccop.jpg";i:2;s:11:"reyborn.png";i:3;s:13:"swopgroup.jpg";}
How can I update this data, for example I want to delete ccop.jpg?
Do not store serialized data in database.
Create a linked table consists of main_id and picture and store your image names in it.
So, you will have distinct access to them.
You have to fetch the value from the database, unserialize it, remove the element, serialize it and save again.
$remove = "ccop.jpg";
//
// get the string from the database
//
$arr = unserialize($str);
foreach($arr as $key => $value)
if ($value == $remove) unset($arr[$key]);
$str = serialize($arr);
//
// save the string back to the database
//
Instead of saving serialized list of values, it's better to have a normalized database and just do a simple DELETE FROM images WHERE object_id = ....
Ideally you need to
extract it
deserialize it
modify it
serialize it
write it back to the database.
Since you are storing it in a VARCHAR field and it is a PHP serialized array you would want to pull it out of the database unserialize and then re-update the field. You shouldn't look to MySQL to modify PHP specific information because well ... that's not what it is made for.
I am using a classified scripts and saves user_meta data in the wp_usermeta table.
The meta_key field is called user_address_info and in it there are all the data like 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 am not using all the fields on the script but user_add1, user_city, user_state and user_postalcode
I am having trouble to get the data using SQL like the example below (wordpress) :
$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_A);
I would like some help here so that I will display anywhere (I dont mind using any kind of SQL queries) the requested info e.g. the user_city of 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";
}
?>
Thank you very much.
No, you can't use SQL to unserialize.
That's why storing serialized data in a database is a very bad idea
And twice as bad is doing serialize twice.
So, you've got nothing but use the code you've given.
I see not much static in it though.
do you experience any certain problem with it?
Or you just want to fix something but don't know what something to fix? Get rid of serialization then
i have found that the serialize value stored to database is converted to some other way format. Since the serialize data store quotes marks, semicolon, culry bracket, the mysql need to be save on its own, So it automatically putting "backslash()" that comes from gpc_magic_quotes (CMIIW). So if you store a serialize data and you wanted to used it, in the interface you should used html_entity_decode() to make sure you have the actual format read by PHP.
here was my sample:
$ser = $data->serialization; // assume it is the serialization data from database
$arr_ser = unserialize(html_entity_decode($ser));
nb : i've try it and it works and be sure avoid this type to be stored in tables (to risky). this way can solve the json format stored in table too.