Using wpdb to connect to a separate database

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

Thanks

Related posts

Leave a Reply

6 comments

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

    $mydb = new wpdb('username','password','database','localhost');
    $rows = $mydb->get_results("select Name from my_table");
    echo "<ul>";
    foreach ($rows as $obj) {
       echo "<li>".$obj->Name."</li>";
    }
    echo "</ul>";
    
  2. 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);
    
  3. no one has said this so I thought I’d add an even easier way..

    as 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);
    
  4. While these will work, you’ll lose the ability to use the “other” custom features such as get_post_custom and wordpress queries. The simple solution is

    $wpdb->select('database_name');
    

    which changes the database system-wide (a mysql select_db). The database.table method works if you just want to make a simple query, but if you want to access another wordpress blog you can use select. You’ll just need to change it back when you’re done or your blog may do strange things.

  5. I can’t comment yet, but I wanted to expand on Wadih M.’s answer (which is great).

    WP’s database class is a customized version of Justin Vincent’s ezSQL. If you like the interface and you’re wanting to do a site that’s not WordPress-based, you might want to check it out: http://justinvincent.com/ezsql

  6. I was struggling with using $wpdb to connect to a second blog database from a parent site that needs to update two blogs. I used $wpdb->select($dbname, $dbh) to select the second database, but I was still getting results from the first database.

    I resolved the problem by calling wp_cache_flush() to clear the WordPress cache before calling WP functions on the second database.