1

I am trying to update data filed in users table.

enter image description here

Please suggest a way how to update this field data with help of query or need to follow some other approach ?

$old_user_id = 123;
$new_user_id = 456;

$account_old = user_load($old_user_id);
$account_new = user_load($new_user_id);

if ($account_old && $account_new && !empty($account_old->data['uc_stripe_customer_id'])) {
  user_save($account_new, array('data' => array('uc_stripe_customer_id' => $account_old->data['uc_stripe_customer_id'])));
}

Thanks!

2 Answers 2

0

I think this would be a good way to do it. You depending what's currently in the data you might want to merge that with the new data. Easy to remove if that's not important.

<?php

$old_user_id = 123;
$new_user_id = 456;

$account_old = user_load($old_user_id);
$account_new = user_load($new_user_id);

// Check to make sure accounts are loaded.
if (empty($account_old) == FALSE && empty($account_new) == FALSE) {

  // make sure account data is populated before trying to act on it.
  if(empty($account_old->data) == FALSE) {

    // unserialize the blob from the user object.
    $account_new_data = unserialize($account_old->data);

    // make sure the key exists. maybe other checks.
    if(array_key_exists('uc_stripe_customer_id', $account_new_data) == TRUE) {

        // merge any existing data with the new data.
        $account_new->data = array_merge($account_new, account_new_data);

        user_save($account_new);
    }
  }
}
1

It's a serialised PHP array.

You would, in my humble opinion, be nuts to try to parse this with SQL.

Use PHP to unserialize, make your changes, serialize it again, and write the result to the database.

3
  • thanks for your answer, please see updated question, is that approach is correct I want to change uc_stripe_customer_id from old account into new one. Please suggest!
    – jas
    Mar 3, 2016 at 16:55
  • Yep, looks like that should work at first glance - are you having any trouble with it in particular?
    – Clive
    Mar 3, 2016 at 17:44
  • It's working fine was just not sure that it's right way!
    – jas
    Mar 3, 2016 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.