Quantcast
Viewing latest article 13
Browse Latest Browse All 27

Access Another Database in WordPress Or Get Data of Another Database in WordPress

In wordpress we usually use the database which we have configured in "wp-config" file and access them using global $wpdb variable. But when situation arise and we require to get data from another database then $wpdb variable will not work.

In this tutorial of php code for beginners we will show you how to access another database or fetch data from another database in WordPress.

To access another database first step is to write down the following code in your theme's function.php file or respective plugin file.

$mydb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST);
$mydb->show_errors();
In above step you have created a variable called "$mydb". Now with the use of this variable you can access the data of your database specified above in any file.

Example:-

<?php
global $mydb; // Don't forget to initialize this
$sql="SELECT * FROM customer";
$result =  $newdb->get_results($sql);
foreach($result as $cust_record)
{
    echo $cust_record->name;
}
?>

Viewing latest article 13
Browse Latest Browse All 27

Trending Articles