I am wondering if it is at all possible to edit an array that was serialized and stored in mysql directly in my mysql?
I am working with a plugin in WordPress that stores an array in mysql (serialized) but I need to change some of the array's values. I was hoping to edit the serialized string in the database but whenever I do the plugin is no longer able to read any of the data.
Is there a way I can edit the serialized data directly without breaking how the plugin reads it?
Cheers!
Unserialize it and then serialize it again. Those are basic php functions.
$array = unserialize($serialized_data);
//do stuff to $array
$serealize = serialize($array);
Related
Hi everyone in the database a column called attachments stores data like this
"a:3:{s:6:\"saveTo\";s:7:\"wpmedia\";s:14:\"attachmentType\";s:6:\"images\";s:11:\"attachments\";a:1:{i:0;a:6:{s:12:\"attachmentId\";i:176165;s:4:\"file\";s:68:\"https://www.yallamission.com/wp-content/uploads/2022/10/MG_00283.jpg\";s:8:\"fileName\";s:12:\"MG_00283.jpg\";s:9:\"thumbnail\";s:76:\"https://www.yallamission.com/wp-content/uploads/2022/10/MG_00283-150x150.jpg\";s:8:\"fileSize\";s:9:\"292.85 KB\";s:8:\"fileType\";s:10:\"image/jpeg\";}}}
i need to fetch the image url only yet i am unable to do it successfully as i don't understand this format
I tried to use php functions and substr() yet the text before and after has dynamic content and no fixed standard.
This is serialized data and you can unserialize them and access the attachments by using unserialize($variable) and access the attachment elements.
Use json_encode instead of serialize when building the data for INSERTing. Then use the JSON functions in SQL (if your MySQL is new enough).
i'm manually reading out database entries from a plugin (eventscalendar wordpress). there are user defined fields / custom fields stored in the database. excuse my ignorance - but i'm having trouble associating the format the data is stored in:
a:1:{s:3:"key";s:6:"extern";}
so in the end all i would need is the value of s:6:"" in a simple variable.
lets say $key = "extern"
any quick solutions?
thanks,
mark
Take a look at unserialize to get it back into an array: http://php.net/manual/en/function.unserialize.php
That is a serialized array-- note the a at the beginning. It also works on Objects.
It is created with PHP's serialize and converted back to proper array/object with unserialize.
It serves a similar purpose to the JSON format.
To give some background, I'm creating a web app using mostly jQuery. I scrape a number of values from a page on another domain; 3 arrays of integers and a date in string format. These values are then displayed on my page. Each of these arrays has 7 numbers.
It makes sense to me to store each array as a single entry in my MySQL database. My original idea was to store the values of each array as a JSON object and insert that into my entry in the DB from reading around SO this doesn't seem like the approach to take. I don't envisage having any need to sort the values at any stage in the future. I will just need the value of each integer and don't need to know how each integer in each array relates to one another.
Can someone point me in the right direction regarding the storing and retrieval of these values?
Usually arrays are kind of iffy to handle. In most cases arrays are values that I need to process later in the front end for the user with javascript (can't think of another use anyway). Therefore what I usually do is store them as text by using
$array = array('one', 'two', 'three');
$textToSaveinDB = implode(',', $array);
When I retrieve this you would just use the other way around
$textFromDB = "one,two,three";
$array = explode(textFromDB, ',')
Afterwards you can do whatever you need to do with the array. For example use JSON_encode() and send it to the user via ajax.
Maybe serialize() and unserialize() would help you. But I won't suggest this as the best approach.
O.K. so I'm pretty clever - I've made a library for keeping a bunch of WP themes that are all set up to my needs so when I put out a new site I can just create the new blog in a few minutes.
As a holder for the new domain everything in the sql file that has the old domain in it I replace with [token].
Everything was working fine right up to the point where I made one with a child theme that apparently serialized data before entering it into the database. End up with stuff like this:
Theme','a:8:{s:12:\"header_image\";s:92:\"http://[token]wp-content/uploads/2011/05/494-Caring-for-fruit-trees-PLR.jpg\";s:16:\"background_image
So I dig into serialization and it turnss out that s:92 bit for example is the number of characters in that value. Since i'm changing [token] it changes and breaks.
Up till now I did all my changes to the sql file and didn't edit the database other than to populate it with the sql file - but I'm at a loss on how to deal with the data in the serial array.
Any ideas?
Easiest way would be to grab that data and use the unserialize() function, like
$arr = unserialize($data);
Then edit the data that way. When you're done, re-serialize it with serialize(), and store it back. You may have to to do a print_r() on the unserialized data to see how it's stored to see what you need to edit.
If you do the changes directly from the serialized data, you'll have to get the length of the current substring, make the change, then get the new length and splice that back into the serialized data, which is way more complicated than it needs to be.
I want to know the way of passing multidimensional in URL with php.
I have an array like this
$number = $_SESSION["number"];
$number = $number+1;
$_SESSION["number"] = $number;
$_SESSION['count']$number]=array($_POST['buy_app_page'],$_POST['x'],$_POST['y'],$_POST['w'],$_POST['h'],$_POST['selected_values'],$number);
$pixels_detail=$_SESSION['count'];
$pixels_detail=$_SESSION['count'];
I want to pass the session data stored in the $pixels_detail variable to url. I tried to to this but it show a blank parameter without any value in the url.
Actually am storing cart data in an session array and have two buttons when the user done adding products he/she clicks on the continue button this is where I want to the whole session data to be passed to the next page in any way, using url or someother I haven't any idea now!
Please Help.
You can pass arrays through URL using the following notation:
somepage.php?testarray[0]=element_one&testarray[1]=element_two
Similarly, you can send multiple arrays like this:
somepage.php?testarray[0][0]=element_one&testarray[0][1]=element_two&testarray[1][0]=element_three&testarray[1][1]=element_four
I tested it locally and it works just fine.
NOTE: Sending lots of content this way is bad practice. I would examine other methods if I were you, that work through POST.
Why do you want to pass data to the URL? Usually storing it in the session is the best way to do it. If you want to pass complex data in the URL you might have a look at the serialize() and unserialize() functions of PHP.
There is also the really nice function http_build_query() that converts complex data. But be aware of the 4096 character limit of a query string. I would really recommend to read the data from the session as far as you don't have any argument against it. You might pass only one parameter with the button and then read the corresponding data from the session.