How to update serialize data in MySQL - php

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.

Related

Codeigniter : append record in mysqli

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.

Retrieve a json field from mysql field with PDO

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));

Inserting an array into a mysql database column

I am trying to insert values from a multi select drop down in a form to a mysql db column. For example: The drop down would have one or more choices chosen then when the form is posted it would insert the data into one column in a mysql db. I'm stuck at how to insert the data.
If you want to insert in single row then you can use implode() to generate comma separated data, or you can do json_encode() and add to your colum.
Say you get the data as
$data = array("one", "two", "tree");
// output one, two, three
$insert_data = implode(",", $data);
or
$insert_data = json_encode($data);
Thats for inserting data in single column. While retrieving you can do explode() or json_decode() to get the return data and can use them in the multi-select again.
If you want one row for each item then just loop through the array and add them
you can turn the array into a single string with http://us1.php.net/function.implode
$comma_separated = implode(",", $array);
set the type of the column to string, then use the function serialize($array) to convert the array into a string. When you want to get the string back to an array, use unserialize($string)
A couple of things to think about:
If there is a one to many relationship - it shouldn't be in one column, look into multiple tables and changing your database structure.
If you really want to pass in an array you will need to convert it to string using the php built in function implode, then using the built in function explode to retrieve the column from the db
$arr = array('val1','val2');
$string = implode(',',$arr);
//Do db insert
//Do db retrieve
$arr = explode(',',$string);
http://php.net/function.implode

unserialize data from mysql

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);

Unserialize values from mySQL

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.

Categories