string / array format convert with php - php

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.

Related

How to edit serialized content in mysql

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

Rails multi-array format in mysql

Have an old project that someone did in PHP and am attempting to convert it over to Rails. Am encountering an issue when trying to iterate through a multiple array in rails.
The array as stored in the DB looks like this:
a:5:{i:0;s:8:"Director";i:1;s:11:"Shareholder";i:2;s:14:"Vice President";i:3;s:9:"Secretary";i:4;s:9:"Treasurer";}
Am trying to display the String values such as "Director" and "Shareholder".
Does the DB field need to be changed to a different format to work?
How would this be done in rails?
Thank you in advance
That looks like the output of PHP's serialize function to me.
If you have PHP around or don't mind installing it to help with the data migration, then I'd write a short PHP script to convert those columns to JSON using PHP's unserialize and json_encode functions: read a value, unserialize it, json_encode the result, write it back into the database (all in PHP).
After that, you should be able to use ActiveRecord's serialize with JSON as the storage format:
class Model < ActiveRecord::Base
serialize :whatever_it_is_called, JSON
end
You could also use YAML (the default for ActiveRecord's serialize) if you had YAML support in PHP-land.
If you don't want to touch PHP at all, then you'll have to write Ruby parser for PHP's serialize format (which seems to be well documented in the PHP docs) and hook that up to ActiveRecord's serialize.
You will of course be working with a copy of the real database for all this work.

Storing an array in a MySQL table

I have a 5 level multidimensional array. The number of keys in the array fluctuates but I need to store it in a database so I can access it with PHP later on. Are there any easy ways to do this?
My idea was to convert the array into a single string using several different delimiters like #* and %* and then using a series of explode() to convert the data back into an array when I need it.
I haven't written any code at this point because I'm hoping there will be a better way to do this. But I do have a potential solution which I tried to outline below:
here's an overview of my array:
n=button number
i=item number
btn[n][0] = button name
btn[n][1] = button desc
btn[n][2] = success or not (Y or N)
btn[n][3] = array containing item info
btn[n][3][i][0] = item intput type (Default/Preset/UserTxt/UserDD)
btn[n][3][i][1] = array containing item value - if more than one index then display as drop down
Here's a run-down of the delimiters I was going to use:
#*Button Title //button title
&*val1=*usr1234 //items and values
&*val2=*FROM_USER(_TEXT_$*name:) //if an items value contains "FROM_USER" then extract the data between the perenthesis
&*val3=*FROM_USER(_TEXT_$*Time:) //if the datatype contains _TEXT_ then explode AGAIN by $* and just display a textfield with the title
&*val4=*FROM_USER($*name1#*value1$*name2#*value2) //else explode AGAIN by $* for a list of name value pairs which represent a drop box - name2#*value2
//sample string - a single button
#*Button Title%*val1=*usr1234&*val2=*FROM_USER(_TEXT_$*name:)&*val3=*FROM_USER(_TEXT_$*date:)&*val4=*FROM_USER($*name1#*value1$*name2#*value2)
In summary, I am seeking some ideas of how to store a multidimensional array in a single database table.
What you want is a data serialization method. Don't invent your own, there are plenty already out there. The most obvious candidates are JSON (json_encode) or the PHP specific serialize. XML is also an option, especially if your database may support it natively to some degree.
Have a look at serialize or json_encode
The best decision for you is json_encode.
It has some advantages for json_encode beside serialize for storing in db.
taking smaller size
if you
must modify data manually in db there will be some problems with serialize, because this format stores size of values that has been serialized and modifying this values you must count and modify this params.
SQL (whether mySQL or any other variant) does not support array data types.
The way you are supposed to deal with this kind of data in SQL is to store it across multiple tables.
So in this example, you'd have one table that contains buttonID, buttonName, buttonSuccess, etc fields, and another table that contains buttonInputType and buttonInputValue fields, as well as buttonID to link back to the parent table.
That would be the recommended "relational" way of doing things. The point of doing it this way is that it makes it easier to query the data back out of the DB when the time comes.
There are other options though.
One option would be to use mySQL's enum feature. Since you've got a fixed set of values available for the input type, you could use an enum field for it, which could save you from needing to have an extra table for that.
Another option, of course, is what everyone else has suggested, and simply serialise the data using json_encode() or similar, and store it all in a big text field.
If the data is going to be used as a simple block of data, without any need to ever run a query to examine parts of it, then this can sometimes be the simplest solution. It's not something a database expert would want to see, but from a pragmatic angle, if it does the job then feel free to use it.
However, it's important to be aware of the limitations. By using a serialised solution, you're basically saying "this data doesn't need to be managed in any way at all, so I can't be bothered to do proper database design for it.". And that's fine, as long as you don't need to manage it or search for values within it. If you do, you need to think harder about your DB design, and be wary of taking the 'easy' option.

What does it mean to serialize data or an object?

using php, if possible.
What does it mean? I was reading one of my old questions ( How do you pass values between PHP pages for MVC?)
and in one of the answers it says:
The part responsible for transferring the data between the controller and the view is the View engine (or class) internal to CodeIgniter. It takes that array from the controller and deserializes it for the view.
I don't know what that means (I read the comments). I put CodeIgniter as the example and tag, but I guess it could be a general question.
Thanks.
To serialize data is to generate a storable representation of a value as a string, for example:
json_encode is a type of serialization but PHP has native support also of serialize that can serialize almost any type of data except resources types, you can find a small guide to serialization here:
http://www.devshed.com/c/a/PHP/The-Basics-of-Serializing-Objects-in-PHP/
For the full manual that corresponds to PHP you can find it in the link that Cédric Belin posted in the post below :D
Serializing usually means converting object (or complex object structure) into text/binary form, suitable for storing or transmitting over network.
Deserialization is a reverse process.
See this link
Serialization is the process of converting an object or an object graph in linear sequence of bytes for storage or transmission to another location.
If you want to store your data in memory or transmit over computer network and reconstruct later in different environment, first you need to convert this data into understandable/usable format by computers which is binary format (00011010101011...).
SERIALization is a convertion of data structure into SERIES of bits.

PHP Serialized database entries messing me all up

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.

Categories