I want to connect wpdb
to another database. How do I create the instance and pass it the database name/username/password?
Thanks
I want to connect wpdb
to another database. How do I create the instance and pass it the database name/username/password?
Thanks
Yes it's possible.
The wpdb object can be used to access any database and query any table. Absolutely no need to be Wordpress related, which is very interesting.
The benefit is the ability to use all the wpdb classes and functions like get_results
, etc so that there's no need to re-invent the wheel.
Here's how:
$mydb = new wpdb('username','password','database','localhost');
$rows = $mydb->get_results("select Name from my_table");
echo "<ul>";
foreach ($rows as $obj) {
echo "<li>".$obj->Name."</li>";
}
echo "</ul>";
global $wpdb
. But before firing $wpdb->get_results method, you must include wp-load.php as: require_once('/your/wordpress/wp-load.php');
Commented
Sep 19, 2015 at 22:25
$mydb->set_prefix('wp_');
$mydb
variable with a new object could leave a connection open (I could be wrong). I would check to see if $mydb
is already instantiated from a previous call, and if so, close the connection before spinning up a new instance. eg (sorry can't do neat Markdown code blocks in the comments): if ($mydb != null) { $mydb->close(); }
Connecting to a second database is easy in WordPress, you simply create a new instance of the WPDB class and use it the same way you would use the standard $wpdb instance we all know and love.
Assuming the second database has the same login information as the main WP one you can even use the predefined constants from wp-config.php to avoid hardcoding the login information.
/**
* Instantiate the wpdb class to connect to your second database, $database_name
*/
$second_db = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST);
/**
* Use the new database object just like you would use $wpdb
*/
$results = $second_db->get_results($your_query);
$second_db->set_prefix('wp_');
no one has said this so I thought I'd add an even easier way..
as long as your additional database has the same user/pass details to access it as your wordpress database you can use the database name before the table name like this
$query = $wpdb->prepare('SELECT * FROM dbname.dbtable WHERE 1');
$result = $wpdb->get_results($query);
SELECT
. You can't insert data.
Commented
Jun 28, 2015 at 15:06
While these will work, you'll lose the ability to use the "other" custom features such as get_post_custom and wordpress queries. The simple solution is
$wpdb->select('database_name');
which changes the database system-wide (a mysql select_db). The database.table method works if you just want to make a simple query, but if you want to access another wordpress blog you can use select. You'll just need to change it back when you're done or your blog may do strange things.
wp_get_post_terms()
doesn't seem to use the newly selected DB?? Every other function I've tried (like get_post_meta()
, get_posts()
etc) seems to work just fine but wp_get_post_terms()
seems to work towards the DB_NAME
database. Any ideas?
Commented
Jul 9, 2013 at 2:54
I can't comment yet, but I wanted to expand on Wadih M.'s answer (which is great).
WP's database class is a customized version of Justin Vincent's ezSQL. If you like the interface and you're wanting to do a site that's not WordPress-based, you might want to check it out: http://justinvincent.com/ezsql
I was struggling with using $wpdb
to connect to a second blog database from a parent site that needs to update two blogs. I used $wpdb->select($dbname, $dbh)
to select the second database, but I was still getting results from the first database.
I resolved the problem by calling wp_cache_flush()
to clear the WordPress cache before calling WP functions on the second database.