Access Another Database in WordPress

database bgWhen accessing the database in WordPress, you need to use the global variable $wpdb. This will take the constants you set in wp-config.php to connect to your database and retrieve whichever results you specify. But what if you have a second database you wish to connect to for whatever reason? To do that all you need is the following snippet:
$newdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST);
$newdb->show_errors();
Add the code snippet to your functions.php file or a specific page template, and then you can use $newdb just like you would $wpdb to access a second database and retrieve some data.
Search

Popular Posts

Small Biz Website Tips Newsletter

Stay up to date with the latest marketing, sales, and service tips and news.

Small Biz Website Tips Newsletter

Stay up to date with the latest marketing, sales, and service tips and news.