Rest API vs wpdb to connect to a separate database on one server


Recommended Posts

hello dear experts, good day, 


i want to connect two pages  on which a plugin like

- participants_database or 

- wp-job-manager runs 


i want to do that via wpdb to another database :: how to combine two wp-pages - here i want to discuss a approach that works  - see


If they are all on the same server under the same hosting account,  that is helpful. In a case like that, you could write your custom code to access the other site’s database directly. This is discussed here, and it’s quite feasible:  link: Using wpdb to connect to a separate database


see here the discussion of several approaches - which is so fruitful that i want to share it with you... - see


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

Answer1: 134 votes  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:



comment 1: you can also save time by using

 global $wpdb

. But before firing ;

$wpdb->get_results method,;

you must include wp-load.php as

: require_once('/your/wordpress/wp-load.php');

comment 2: Set WPDB prefix to make WP_Query and get_post to generate correct sql query by calling $mydb->set_prefix('wp_');


answer2: [ 30 votes ] 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);


comment 1: 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. –

comment 2: Set WPDB prefix to make WP_Query and get_post to generate correct sql query by calling



answer3:[ 21 votes ] no one has said this so I thought I'd add an even easier 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);

comment 1: From my experience, this only works to get data, i.e. using SELECT. You can't insert data.

comment 2: it will not work externally,

  - see more


Conclusio: well i like this approach very very much.  The good thing - i am with the sites in question - i am on the same server - so i guess that this will work for me.




just wanted to share this with you 

any idea any experience !? 

love to hear from you



Link to comment
Share on other sites

some musings: 


If the sites are all on the same server then we just cam connect to the same database from each site, that's all there is to it.  If the sites are on different servers then we would need to build an API, but  - if they're (luckily) on the same server then just connect to whatever database we want to connect to.  There are no rules. We can connect to a database - and yes - we also can use a db object to connect to more than only one db. PHP doesn't care how many database connections we have. 



Link to comment
Share on other sites

This topic is now closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.