0

I've literally spent half a day trying to find a solution for this problem.

From one WordPress installation I need to query multiple other WordPress installations' databases.

I'd very much like to use the $wpdb way, as this is what I've always used (just haven't ever had the need to access more than one database).

I've created a wpdb object by using this method:

$new_db_connection = new wpdb(*DB INFORMATION INSERTED HERE*)

However, as stated on WordPress' own site, only one wpdb connection can exist at a time.

The $wpdb object can talk to any number of tables, but only to one database at a time; by default the WordPress database. In the rare case you need to connect to another database, you will need to instantiate your own object from the wpdb class with your own database connection information.

My question to you guys is: How do I create a new wpdb object after the first? It doesn't work to unset() the first database.

My workflow is: Query one DB, get data and store in variable, query next DB, get data and store in variable ... etc.

Edit: My question isn't a duplicate, since I'm looking for a way to create more than one new wpdb object.

I need to create a new wpdb object, access the external DB through it and store result in variable, then create ANOTHER new wpdb object and do the same and so on.

However, it only works for the first new wpdb object. It has to be because of the quote from WordPress' site. If I comment out the first wpdb object then the second one works.

7
  • Sorry but what error are you getting by creating the wpdb object? It seems fine to me.
    – Laxmana
    Commented Apr 26, 2017 at 14:04
  • 1
    Possible duplicate of Using wpdb to connect to a separate database
    – cjbj
    Commented Apr 26, 2017 at 15:03
  • As an alternative, you could consider using the REST API to pull in the other data.
    – WebElaine
    Commented Apr 26, 2017 at 15:32
  • It's only when creating the SECOND new DB connection, that it doesn't work. Commented Apr 26, 2017 at 19:32
  • This is NOT a duplicate of that question. I know how to create a new wpdb object. However, create a second, third, fourth etc. wpdb object in the same code doesn't work Commented Apr 26, 2017 at 19:37

3 Answers 3

1

The $new_db_connection is your new db object.

Just use that to access the other db, ie.

$new_db_connection->get_row("your query");
1
  • That's what I'm doing. And it works. For the FIRST external DB. When I try to create another new DB connection, it doesn't work. Commented Apr 26, 2017 at 19:31
0

Sounds like a caching issue; calling wp_cache_flush() between each subsequent connection should resolve the issue.

On a side note:

However, as stated on WordPress' own site, only one wpdb connection can exist at a time.

I've seen numerous people misinterpret this portion of the documentation referenced by the OP, so it's worth pointing out the difference between the wpdb class and the global $wpdb object. One can instantiate as many instances of the wpdb class as they like for numerous disparate database connections. One instance per connection. Just don't try connecting that global $wpdb instance to another database and expect the original connection to magically keep working.

From the WordPress docs:

By default, WordPress uses this class to instantiate the global $wpdb object, providing access to the WordPress database.

...

An instantiated wpdb class can talk to any number of tables, but only to one database at a time. In the rare case you need to connect to another database, instantiate your own object from the wpdb class with your own database connection information.

0

I was curious about this too, so I dug into the WordPress source to see where new wpdb was found.

I found it here in load.php:

$dbuser     = defined( 'DB_USER' ) ? DB_USER : '';
$dbpassword = defined( 'DB_PASSWORD' ) ? DB_PASSWORD : '';
$dbname     = defined( 'DB_NAME' ) ? DB_NAME : '';
$dbhost     = defined( 'DB_HOST' ) ? DB_HOST : '';

$wpdb = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost );

All you'd need to do is rename $wpdb to wherver you wanted to call it which was $new_db_connection.

Your Answer

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

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