How to access another Database in WordPress

by in on

wpdb is the class that helps you to manipulate the database in WordPress. But you can not access it directly, you have to use the global object $wpdb to manipulate the database. The Database configuration is stored in wp-config.php during installation process. Highlighted like this

 

How to add another Database?

$wpdb object is reserved with the default Database configuration, So if we required to access another Database in WordPress then simply add the Database information in the wpdb class.

 

With this you have successfully established a new Database connection and the same WordPress query works with your newly created Object. You can on errors echoing with $wpdb->show_errors() or if you want to print the error(If any) by the most recent query with $wpdb->print_error().

  • Chris Beveridge

    So does this then allow you to query both dB’s at the same time…or just move to the newer one?

    • Yes this is allow you to query on both DB’s at the same time.