Title: Remote database data manipulation
Last modified: August 26, 2024

---

# Remote database data manipulation

 *  [pwpx2](https://wordpress.org/support/users/pwpx2/)
 * (@pwpx2)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/remote-database-data-manipulation/)
 * Is it possible to manipulate data from your local database (username/passwords/
   email/etc) and insert them to a remote different database ?
 * I’m trying to do this but i don’t have much success.
 * Any help ?
 * P.S I did pulled the data using the `get_userdata($user_id);`
 * I did make another wpdb instance to the remote database using and it’s variables
 * `$other_db = new wpdb($other_db_user, $other_db_password, $other_db_name, $other_db_host);`
 *     ```wp-block-code
       the entire code looks something like thisrequire __DIR__ . '/wp-load.php';define( 'WP_DEBUG', true );define( 'WP_DEBUG_LOG', true );global $wpdb;$other_db = new wpdb($other_db_user, $other_db_password, $other_db_name, $other_db_host);if (!$other_db) {echo $wpdb->show_errors();}else{echo 'Connected successfully';}// Get user data from the local database$user = get_userdata($user_id);$external_users = $other_db->'users';$wpdb->insert($external_users,array('username' => $user->user_login,'hash' => $user->user_pass,'email' => $user->user_email,));
       ```
   
 * when i try to run the file with the code i just get blank page suspecting its
   some kind of error which doesn’t print nor doesn’t show in the debug.log
    -  This topic was modified 1 year, 9 months ago by [pwpx2](https://wordpress.org/support/users/pwpx2/).

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [astroo](https://wordpress.org/support/users/astroo/)
 * (@astroo)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/remote-database-data-manipulation/#post-17975723)
 * From your description and code, it seems you want to transfer user data from 
   a local WordPress database to a remote database. Your approach is almost correct,
   but there are a few issues that could be causing the blank page or preventing
   the code from working. Key Points to Check and Suggestions:
    1. **Check for PHP Errors:**
       Blank pages often indicate a PHP error. Even though
       you have enabled debugging, PHP errors might not be outputted correctly. You
       can add the following code at the top of your file to ensure all errors are 
       displayed:
 *     ```wp-block-code
          ini_set('display_errors', 1);
          ini_set('display_startup_errors', 1);
          error_reporting(E_ALL);
       ```
   
    2. **Syntax Error in Your Code:**
       There is a mistake in this line:
 *     ```wp-block-code
          $external_users = $other_db->'users';
       ```
   
 * It should be:
 *     ```wp-block-code
          $external_users = $other_db->prefix . 'users'; // Assuming 'users' table has a prefix
       ```
   
 * or simply:
 *     ```wp-block-code
          $external_users = 'users';
       ```
   
 * The `->` operator cannot be used to directly access a string. You need to correct
   that.
    3. **Inserting Data into the Remote Database:**
       The `wpdb::insert()` method should
       be called on the correct `$other_db` instance, not the local `$wpdb` instance.
       Your code should look like this:
 *     ```wp-block-code
          $other_db->insert(
              $external_users,
              array(
                  'username' => $user->user_login,
                  'hash'     => $user->user_pass,
                  'email'    => $user->user_email,
              )
          );
       ```
   
    4. **Database Table Structure:**
       Ensure that the remote database has the exact 
       table structure for the `users` table and that the column names match. For example,
       if the column names are `user_login`, `user_pass`, and `user_email`, use those
       exact names.
    5. **Debugging:**
       You can add logging to see if your code runs up to a certain 
       point. For example:
 *     ```wp-block-code
          error_log('Reached this point before inserting data.');
       ```
   
 * Check the `debug.log` file in the `wp-content` directory for the logged messages.
    6. **Database Connection Validation:**
       You’re currently checking the connection
       like this:
 *     ```wp-block-code
          if (!$other_db) {
              echo $wpdb->show_errors();
          }
       ```
   
 * However, `$other_db` is an object, so this check will always pass. Instead, validate
   the connection using the `last_error` property:
 *     ```wp-block-code
          if ( $other_db->last_error ) {
              error_log('Database connection error: ' . $other_db->last_error);
          } else {
              echo 'Connected successfully';
          }
       ```
   
    7. **Avoid Hardcoding the Table Name:**
       WordPress tables usually have prefixes (
       like `wp_`). Use the `$wpdb->prefix` property to handle this dynamically if 
       needed.
 * Final Revised Code Example:
 *     ```wp-block-code
       require __DIR__ . '/wp-load.php';
       define( 'WP_DEBUG', true );
       define( 'WP_DEBUG_LOG', true );
   
       ini_set('display_errors', 1);
       ini_set('display_startup_errors', 1);
       error_reporting(E_ALL);
   
       global $wpdb;
   
       $other_db = new wpdb($other_db_user, $other_db_password, $other_db_name, $other_db_host);
   
       if ( $other_db->last_error ) {
           error_log('Database connection error: ' . $other_db->last_error);
       } else {
           echo 'Connected successfully';
       }
   
       $user = get_userdata($user_id);
   
       if ( $user ) {
           $external_users = 'users'; // Adjust the table name as needed
   
           $result = $other_db->insert(
               $external_users,
               array(
                   'username' => $user->user_login,
                   'hash'     => $user->user_pass,
                   'email'    => $user->user_email,
               )
           );
   
           if ( $result === false ) {
               error_log('Insert error: ' . $other_db->last_error);
           } else {
               echo 'Data inserted successfully.';
           }
       } else {
           error_log('User not found.');
       }
       ```
   
 * Additional Tips:
    - Make sure the remote database user has the correct permissions to insert data.
    - Ensure that the remote table structure and column names match the data you’re
      inserting.
    - Check the `debug.log` file and PHP error output for clues if the issue persists.
 * This should resolve your issues and help in successfully inserting data into 
   the remote database.
 *  Thread Starter [pwpx2](https://wordpress.org/support/users/pwpx2/)
 * (@pwpx2)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/remote-database-data-manipulation/#post-17976062)
 * Thank you. Will try the updated code when i arrive back home.
 * So to make it clear…
 * This code will take the data i need from the local database and put it on the
   remote one ? Or just into the remote one ? Because i do need it to get as normal
   on the local database also.
 * The entire process i want to do is to just retrive some of the data from the 
   local database into the remote one. You might call it as a small backup. (using
   cron eventually)
 *  Thread Starter [pwpx2](https://wordpress.org/support/users/pwpx2/)
 * (@pwpx2)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/remote-database-data-manipulation/#post-17976257)
 * Tried the code and it seems it makes some confusing. He thinks the remote db 
   connection details to use on the local database and gives me access denied on
   my local database and undefined $user_id because cannot access the local database
   to pull out the data.
 * That’s why i’ve tried to pull the local data, then make another wpdb instance
   for the remote database and then insert the data pulled from local to remote 
   one.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Remote database data manipulation’ is closed to new replies.

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [Remote](https://wordpress.org/support/topic-tag/remote/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [pwpx2](https://wordpress.org/support/users/pwpx2/)
 * Last activity: [1 year, 9 months ago](https://wordpress.org/support/topic/remote-database-data-manipulation/#post-17976257)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
