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?

link|improve this question

80% accept rate
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... – One Trick Pony 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? – One Trick Pony 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
feedback

1 Answer

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...

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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