WordPress working with Two Database Simultaneously
X

Congrats, You are Subscribed to Receive Updates.

WordPress working with Two Database Simultaneously


Now a days we need more than one database is necessary for certain large volume site projects. Especially, if you are working with user management and large data centers. So here I am going to play something to work with more than one database simultaneously.  Yes, its possible in WordPress, But you need to be more careful on writing of codes and have some additional works to carry out both database. Lets move to the topic.

wp-two-db

While installing your WordPress CMS Your  are required to enter a database details to create your WordPress files in it. The database works to create post and user details so it will work in default. Now, We are going to create one another database and adding some custom details into it,

Just move to your themes directory and open current active theme folder , than open ” functions.php ” .  and here we are going to create a new object for existing wpdb Class with the following Code,

global $kvdb; 

$kvdb = new wpdb('kv_db_username', 'kv_db_user_password', 'kv_second_db_nam', 'db_host');

The above code creates another object with new database. No , we can do all the Wpdb operations without and problem. Also you can use it anywhere with declaring a global variable, ” $kvdb ” . Here i am going to write a simple function to get information from another database table.

$users = $kvdb->get_results( "SELECT * FROM `kvdb_users`");
						
foreach ( $users as $user ) {
	echo $user->user_login .'<br>'; 
}

The above function will get usernames of another WordPress database. Not only the another WordPress database, you can access any database with it.  But remember , your access is limited and you cant create new table with it.

I created a plugin for WordPress to play with second database.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, OpenCart Theme Development, PHP, HTML, CSS, jQuery, Bootstrap development and content SEO.

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar

Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.

Advertisement

Category Posts