Suppose you have a secondary database that you need to connect to, but you’d also like to use the methods in the WPDB object to interact with the database in the same way that you work with the primary WP database. Well, here’s a very simple way to do that.
global $odb;
$db_user = "Your DB Username";
$db_pass = "Your DB Password";
$db_name = "Your DB Name";
$db_host = "Your DB Hostname";
$odb = new wpdb( $db_user, $db_pass, $db_name, $db_host );
Now, you can use the global $odb (in this example) the same way you would use $wpdb. You can call this variable anything you want – I just used “odb” for “other database”, but use what makes sense in your situation.
Leave a Reply