Apr
04
2011

Access Another Database in WordPress

by   |  Posted in Tutorials  |  22 comments

When accessing the database in WordPress, you need to use the global variable $wpdb. This will take the constants you set in wp-config.php to connect to your database and retrieve whichever results you specify. But what if you have a second database you wish to connect to for whatever reason? To do that all you need is the following snippet:

$newdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST);
$newdb->show_errors();

Add the code snippet to your functions.php file or a specific page template, and then you can use $newdb just like you would $wpdb to access a second database and retrieve some data.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
Share the love...

Tags: , , , , ,

Short URL: http://bit.ly/hA921q

Discussion 22 Comments

  1. Joachim Kudish on April 4, 2011 at 8:53 pm

    Great tip! I knew that there was an API to do that but never realized it was that easy!

  2. Pothi on April 5, 2011 at 2:39 am

    That is a lovely tip. I can see how many different ways it can be used without messing up the default database for WP.

    • Konzerte on August 6, 2011 at 8:44 am

      Absolutely, Thanks Bavotasan. I used your trick to access parts of the same database from two different sites. Works like a charm!

  3. Wanda on April 5, 2011 at 4:09 am

    So simple yet so helpful. Thanks!

  4. Theron on April 5, 2011 at 3:51 pm

    What would be a few real world examples that you’d use this for? Can you elaborate perhaps on a few thing’s you’ve done with it?

    • c.bavota on April 6, 2011 at 10:26 am

      I’m currently working on a WordPress site that needs to access data from a second database to create some lists. This is the functions I am using.

  5. Forex on April 7, 2011 at 6:43 am

    Maybe I will use this tip in the future, thanks

  6. Ed on April 10, 2011 at 9:38 am

    Could this be done with an external database on a different domain as well?

    • c.bavota on April 10, 2011 at 9:46 am

      Yup. I used it to gather information from a domain on a totally different server.

  7. Konto Internetowe on April 11, 2011 at 3:08 pm

    Thanks man for that tip, it is gonna be useful

  8. Edwin on May 12, 2011 at 10:42 am

    Thanks for the tip.

    Could this be used to pull in post from another WordPress site?

    I want to build a kind of portal that displays posts from two existing sites of mine.

    • c.bavota on May 13, 2011 at 10:11 am

      Yes it could. That is what I have used it for.

  9. David Tan on May 19, 2011 at 2:40 pm

    wrote about something similar back in my post http://www.davidtan.org/wordpress-how-to-use-wpdb-class-to-connect-fetch-data-from-another-database/ but didn’t know that can be inserted into functions.php too thanks for the tips :)

  10. Konzert on June 23, 2011 at 4:04 am

    so it is possible to grab data from another server, or is it just on the same host?

    • c.bavota on June 23, 2011 at 12:10 pm

      You can do it from another server.

  11. Thomas George on July 18, 2011 at 2:38 am

    Great tip! Thanks!!

  12. Chris on July 28, 2011 at 5:00 pm

    Do you have to change it back?

    • c.bavota on August 1, 2011 at 12:57 pm

      Nope. You are just setting a different variable with the new db info.

  13. Jen on August 1, 2011 at 4:10 pm

    Can you give an example of how this would work with a custom query as well? I have two sites sharing a server but with separate DBs. I’d like to bring in the 3 latest posts from one DB (blog) into the 2nd. I’ve found a few examples online with this method but none of them seem to be working. Either I’m getting something wrong, or it’s not doing what I was hoping for.

    TIA!

  14. Jen on August 2, 2011 at 11:46 am

    Thanks, the only real issue I have with using RSS is styling things the way I want and importing an image as well.

  15. James on September 7, 2011 at 10:25 pm

    Your the MAN! This helped so much, thank you

    Regards
    James