How to Connect multiple database in WordPress?

Hi, i am looking for some example code that help me to connect multiple database in WordPress which include class that handles all the database related tasks. While working on PHP projects I have faced many issues related with php questions & answers, so I am hoping for valuable suggestions & great support from the online community.

Why do you need multiple databases - WordPress is designed to be able to run thousands of sites from a single database.

Yes you can do anything with PHP :grinning: but what is your purpose.
You could just create another config.php file and connect from there?

Example below is calling the table $mycities from $wpdbtest_otherdb

$wpdbtest_otherdb = new wpdb('myaccount_cityuser', 'mypassword', 'myaccount_cities', 'localhost');
$mycities = $wpdbtest_otherdb->get_results("SELECT name
                                            FROM city
                                            WHERE city.stadium = 1");
foreach ($mycities as $mycity) {
    echo $mycity->name . '<br />';

Although the above example is dirty and not the most secure yet would call the data from the other DB as you might needโ€ฆ

PS Be more precise in your question.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.