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:
- 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:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
- Syntax Error in Your Code:
There is a mistake in this line:
$external_users = $other_db->'users';
It should be:
$external_users = $other_db->prefix . 'users'; // Assuming 'users' table has a prefix
or simply:
$external_users = 'users';
The ->
operator cannot be used to directly access a string. You need to correct that.
- 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:
$other_db->insert(
$external_users,
array(
'username' => $user->user_login,
'hash' => $user->user_pass,
'email' => $user->user_email,
)
);
- 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.
- Debugging:
You can add logging to see if your code runs up to a certain point. For example:
error_log('Reached this point before inserting data.');
Check the debug.log
file in the wp-content
directory for the logged messages.
- Database Connection Validation:
You’re currently checking the connection like this:
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:
if ( $other_db->last_error ) {
error_log('Database connection error: ' . $other_db->last_error);
} else {
echo 'Connected successfully';
}
- 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:
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.