Unserialize and change value, then re-serialize and update

Hi,
    I am trying to extract a serialised field in a MySQL database, put it into PHP, unserialise and make a change to a value called status - I need to update the status field from Pending to Active, then re-serialise and re-upload back up.

How can I do this?

Damian
damianb123Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
This is too generic to provide a useful answer. It would help to know what you've tried so far. Also, what kind of serialization is being used (if you can provide an example of the data, we can tell) ? Is there a reason the status field is inside serialized data inside the database instead of just being another field in the database table?

The general flow would be to write some PHP to connect to the database, run a SELECT query to get the correct row, get the row, run the deserialization/decoding routing on the value to convert it back into either an object or an array (whatever the original structure was), update the value, serialize/encode it again, and run an UPDATE query  to update the row with the new serialized/encoded data.

You should be able to write most, if not all of that code. It looks like you have other questions where you have almost all of the code that I described above.
0
damianb123Author Commented:
Hi Gr8gonzo,
        Basically this is the code I have so far:

   $db_host        = '11.222.333.444';
   $db_user        = 'dbuser';
   $db_pass        = 'mypassword';
   $db_database    = 'mydatabase'; 
   $db_port        = '3306';

//$link = mysqli_connect($db_host,$db_user,$db_pass,$db_database,$db_port);

mysql_connect($db_host,$db_user,$db_pass) or die ('MySQL Not found // Could Not Connect.');
mysql_select_db("$db_database") or die ("No Database found.");

$result = mysql_query("SELECT meta_value FROM usermeta WHERE meta_key = 'member_options' AND user_id=8076");
$row = mysql_fetch_array($result);
$data = unserialize($row);
var_dump($data);

Open in new window


So the above polls the wordpress database, find the row for user ID 8076 and extracts the meta_value.  The information is stored in a serialised field due to there being about 40 fields stored with the potential for 100,000's of entries - I didn't want unnecessary rows being added, if that makes sense.

This is a snippet of what the serialised data looks like:

a:39:{s:2:"id";i:8076;s:13:"custom_fields";a:20:{s:20:"subscription_options";N;s:5:"title";s:19:"Please select title";s:10:"first_name";s:6:"John";s:9:"last_name";s:7:"Peters";s:5:"email";s:26:"john@john.co.uk";s:14:"address_line_1";s:15:"The Lane";s:14:"address_line_2";s:6:"Baston";s:14:"address_line_3";s:0:"";s:4:"city";s:7:"London";s:6:"county";s:10:"Leicestershire";s:3:"zip";s:7:"LC43 6TY";s:7:"country";s:2:"GB";s:6:"status";s:7:"Pending";s:12:"payment_type";s:0:"";s:13:"autoresponder";s:0:"";s:10:"subscribed";s:0:"";s:22:"autoresponder_notified";s:0:"";s:16:"terms_conditions";i:0;s:21:"terms_conditions_date";s:0:"";}

Open in new window



I think I'm almost there, it's more on how to unserialise and change the status value and reupload?

Thanks

Damian
0
gr8gonzoConsultantCommented:
So that data is using PHP's own serialized format, which is encoded/decoded using serialize() and unserialize(), so it looks like you already have the proper code for deserializing, but your example data is corrupted. It looks like someone tried to replace content inside the string without properly decoding/encoding it.

PHP's serialization format is pretty strict in that it defines both the value AND the expected length and type of the value. So if you look at the data, you'll see stuff like:

s:10:"abcdefghij"

The "s" means that the upcoming value is a string, and should be 10 bytes long, and the value will be abcdefghij.

So if the value changes to something like:

s:10:"abc"

...then that value is now corrupt because "abc" is 3 bytes long, not 10, so PHP would be unable to properly unserialize() it unless you manually fixed the length, too:

s:3:"abc"

So really, you don't want to ever manually edit serialized content - you should always deserialize it, edit it, and re-serialize it. Once it's corrupted, it becomes very tedious to manually fix it. For example, if I manually fix the invalid string lengths on the data you provided to me, and then I break it up into separate lines (for easier visual debugging):

a:39:
{
  s:2:"id"
  i:8076
  s:13:"custom_fields"
  a:20:
  {
    s:20:"subscription_options"
    N
    s:5:"title"
    s:19:"Please select title"
    s:10:"first_name"
    s:4:"John"
    s:9:"last_name"
    s:6:"Peters"
    s:5:"email"
    s:15:"john@john.co.uk"
    s:14:"address_line_1"
    s:8:"The Lane"
    s:14:"address_line_2"
    s:6:"Baston"
    s:14:"address_line_3"
    s:0:""
    s:4:"city"
    s:6:"London"
    s:6:"county"
    s:14:"Leicestershire"
    s:3:"zip"
    s:8:"LC43 6TY"
    s:7:"country"
    s:2:"GB"
    s:6:"status"
    s:7:"Pending"
    s:12:"payment_type"
    s:0:""
    s:13:"autoresponder"
    s:0:""
    s:10:"subscribed"
    s:0:""
    s:22:"autoresponder_notified"
    s:0:""
    s:16:"terms_conditions"
    i:0
    s:21:"terms_conditions_date"
    s:0:""
  }

Open in new window


...so you can see that it's still cut off / missing data at the end. There should be an array of 39 elements, and there's only 4 defined.

But let's say that you have some valid data and you wanted to update that status field, you'd probably do:

$data = unserialize($row["meta_data"]);

Then:
if(isset($data["custom_fields"]) && isset($data["custom_fields"]["status"]))
{
  $data["custom_fields"]["status"] = "Active";
}

Then:

$result = mysql_query("UPDATE usermeta SET meta_value='" . mysql_real_escape_string(serialize($data)) . "' WHERE meta_key = 'member_options' AND user_id=8076");


Also, remember that the mysql_ functions are deprecated - you need to move to mysqli_ as soon as possible. Otherwise, if your PHP engine gets updated to version 7, your code is going to stop working.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
damianb123Author Commented:
Perfect!  Thanks very much for the help, this got me sorted!

Damian
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.