1

I have a Wordpress site that uses two databases -- one section queries one database ("database_A"), and then Wordpress makes its connection to its own database ("database_B").

Everything it working well until I go to call this function:

$pageposts = $wpdb->get_results($querystr, OBJECT);

The Wordpress suddenly selects the wrong database ("database_A") when it was just using ("database_B").

How do I (a) prevent it from selecting ("database_A") or (b) make a call to have it select ("database_B")?

3
  • I think you might need to post some more code around the problem area. The most obvious explanation is that $wpdb is changing, but without more code it's going to be impossible to say why. Commented Dec 27, 2009 at 19:06
  • I admit this is bit of a gray area, but I think this would be better suited on serverfault or superuser Commented Dec 27, 2009 at 19:08
  • 2
    Nope, this is definitely a programming question and belongs here. Commented Dec 27, 2009 at 19:10

2 Answers 2

2

The wpdb class in WP ha a select() method. You should just be able to call it directly.

$wpdb->select('database_B');

You could also instantiate a second object that uses database_b:

$wpdb_b = new wpdb($db_b_user, $db_b_pwd, 'database_B', $db_b_host);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jage! That worked well in reconnecting to the Wordpress Database!
0

You can create a new $wpdb-var, like this:

<?php
$wpdb2 = new wpdb($user, $dbpassword, $db2, $dbhost);
?>

Now you can easily select items from the other database:

<?php
$pageposts = $wpdb2->get_results($querystr, OBJECT);
?>

I hope it helps you :]

(edit: Are you sure it suddenly changes the database? I mean, is it using database A before it is using database B, that's almost impossible...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.