1

I want to connect with another wp db & then fetch records from new connected db then close that db.

I follow How can I connect to another WP database and use WP_Query? but after connect to new db if I print $wpdb it does not show the new db tables. here is the code:

global $wpdb;
$wpdb_backup = $wpdb;
$wpdb = new wpdb( NEWS_DB_USER, NEWS_DB_PASSWORD, NEWS_DB_NAME, NEWS_DB_HOST );
# Do your stuff here...
var_debug($wpdb);
# then when done...
$wpdb = $wpdb_backup;

below variables I defined in wp-config.php

NEWS_DB_USER, NEWS_DB_PASSWORD, NEWS_DB_NAME, NEWS_DB_HOST

and output is:

wpdb Object
(
    [show_errors] => 
    [suppress_errors] => 
    [last_error] => 
    [num_queries] => 0
    [num_rows] => 0
    [rows_affected] => 0
    [insert_id] => 0
    [last_query] => 
    [last_result] => 
    [result:protected] => 
    [col_info:protected] => 
    [queries] => 
    [prefix] => 
    [ready] => 1
    [blogid] => 0
    [siteid] => 0
    [tables] => Array
        (
            [0] => posts
            [1] => comments
            [2] => links
            [3] => options
            [4] => postmeta
            [5] => terms
            [6] => term_taxonomy
            [7] => term_relationships
            [8] => commentmeta
        )

    [old_tables] => Array
        (
            [0] => categories
            [1] => post2cat
            [2] => link2cat
        )

    [global_tables] => Array
        (
            [0] => users
            [1] => usermeta
        )

    [ms_global_tables] => Array
        (
            [0] => blogs
            [1] => signups
            [2] => site
            [3] => sitemeta
            [4] => sitecategories
            [5] => registration_log
            [6] => blog_versions
        )

    [comments] => 
    [commentmeta] => 
    [links] => 
    [options] => 
    [postmeta] => 
    [posts] => 
    [terms] => 
    [term_relationships] => 
    [term_taxonomy] => 
    [usermeta] => 
    [users] => 
    [blogs] => 
    [blog_versions] => 
    [registration_log] => 
    [signups] => 
    [site] => 
    [sitecategories] => 
    [sitemeta] => 
    [field_types] => Array
        (
        )

    [charset] => utf8
    [collate] => 
    [real_escape] => 1
    [dbuser:protected] => root
    [dbpassword:protected] => 
    [dbname:protected] => africac_web
    [dbhost:protected] => localhost
    [dbh:protected] => Resource id #320
    [func_call] => 
    [is_mysql] => 1
)

2 Answers 2

1

here is what I did with slight change as per your need. May be it will helpful for you. :)

function fetch_news()
{
    global $wpdb;
    $wpdb_backup = $wpdb;   // backup existing instance
    $wpdb = new wpdb( NEWS_DB_USER, NEWS_DB_PASSWORD, NEWS_DB_NAME, NEWS_DB_HOST ); // create new db instance
    wp_set_wpdb_vars();     // add variable

    $news_sql = $wpdb->get_results("SELECT * FROM rss_news");       // your query to external db table using $wpdb object
    or
    $news_sql = mysql_query("SELECT * FROM rss_news");      // your query to external db table

    $wpdb = $wpdb_backup;   // restore old instance
    return $news_sql;
}
2
  • can you please let me know how can I close that new db connection? Jun 20, 2013 at 10:12
  • we don't need to worry about to close because before return restored old object with new instance using $wpdb = $wpdb_backup; which automatically destroy all connections bounded with new instance. Jun 20, 2013 at 10:32
0

That's correct, you won't see them. Those table names are coded into the wpdb class, they're not read from the database.

3
  • then who could I access new db tables & query the records & then close connection? Jun 20, 2013 at 6:00
  • you'd just put the table name directly in your query SELECT * FROM your_table_name WHERE .... or you can create your own variables to store them if you're doing lots of queries in various places, it's up to you.
    – Milo
    Jun 20, 2013 at 6:14
  • and how to close that connection? I used mysql_close($wpdb); but it says: Warning: mysql_close() expects parameter 1 to be resource, object given in ... Jun 20, 2013 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.