Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to connect wpdb to another database. How do I create the instance and pass it the database name/username/password?

Thanks

share|improve this question
    
Another MySQL database, or another database type? Do you still want access to the regular WordPress database, or are you moving the site from one DB to another? –  EAMann Sep 10 '10 at 3:39
1  
Yes, another MySQL database. It's a separate DB on the same server, and it's not a Wordpress one. It's a custom db, with information I want to display inside wordpress. –  Wadih M. Sep 10 '10 at 3:41
1  
If you did that with the $wpdb object, if it were even possible, it would disconnect the rest of WordPress from its existing database. So, not recommended. Another option is to create a new instance using EZSQL, which is used by WordPress. I think EZSQL is used because it's a layer that abstracts you from having to use php-pdo-mysql, php-mysql, or php-mysqli, not knowing which might be installed on a given server. –  Volomike Sep 10 '10 at 5:18
2  
Yes it's possible. wpdb can be instantiated to access any database and query any table. –  Wadih M. Sep 10 '10 at 12:43

6 Answers 6

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.

share|improve this answer
    
I'm using this solution and it works great, except for one thing. For some unknown reason 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? –  powerbuoy Jul 9 '13 at 2:54

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.

share|improve this answer

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);
share|improve this answer

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);
share|improve this answer
    
This is somewhat redundant to Wadih's answer but I think my code example is a bit clearer and its also important to remember the db login constant's as they are almost always the right ones to use and otherwise you risk issues when moving from dev->stage->live environments where the login details might change. –  jeremyclarke Sep 11 '10 at 20:55
    
@Jeremy Clarke - Nice, concise answer. –  MikeSchinkel Oct 21 '10 at 8:59

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://www.woyano.com/jv/ezsql

share|improve this answer
    
ezSQL was really frustrating for me, coming from WPDB. No "prepare" statements, no "insert" or "update"... I like to use the entire WPDB class as it exists, which is possible by including a couple files out of BackPress in your project. –  goldenapples Apr 22 '11 at 19:23
    
@gabrielk The link is dead - new one is: [1] [1]: justinvincent.com/ezsql –  Hexodus Nov 23 '13 at 8:20
up vote 42 down vote accepted

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>";
endforeach;
echo "</ul>";
share|improve this answer
1  
Booyah. Too bad all those comments added up on the question itself to block your accurate answer. –  jeremyclarke Sep 11 '10 at 20:46
    
@Jeremy Clarke: I agree. Hoping our fellow wordpressers will be more careful to not innocently spread out disinformation. –  Wadih M. Sep 20 '10 at 16:27

protected by toscho Jun 8 '12 at 11:59

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.