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 have read here about switching the current wp database on the fly using

$wpdb->select('database_name');

Once done, I understand that i will have to issue the same statement to the original database.

Questions:

  • Right after changing the database, do i need to flush caches or similar stuff to get things working properly? Will I need to call again WP header?

  • Potential issues?

share|improve this question
    
I don't think select() will switch the db :) That's used to select tables. Read the question you linked - you need to instantiate a new wpdb object, with the db name as one of the arguments... –  onetrickpony Jun 22 '11 at 10:15
    
It does. I'm testing this and seems working. As for the linked post, please read last posts from Matt & Dennis. –  Riccardo Jun 22 '11 at 10:34
    
But how do you pass the user/password to connect to that database? –  onetrickpony Jun 22 '11 at 10:44
    
I'm using the same user/password for both the databases. Works perfectly! –  Riccardo Jun 22 '11 at 15:41
    
$wpdb->select() is used to switch to a different database on the same server. If your user doesn't have access, it won't work. It internally calls mysql_select_db() and passes in the database name. –  EAMann Jun 22 '11 at 20:26
show 1 more comment

1 Answer 1

up vote 1 down vote accepted

Yes it works!

This is the code, assuming both the databases share the same username & password:

$wpdb->select('mydbname');
wp_cache_flush();

Once done, re-issue the same statements, with the original database name.

Can't see any performance issue at the moment, maybe because this is done when building a custom form picking data from two posts out of two databases. It may impact performance when used on a large amount of rows in batch loop...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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