Connect another DB and fetch records from some tables

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:

Read More
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
)

Related posts

2 comments

  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. That’s correct, you won’t see them. Those table names are coded into the wpdb class, they’re not read from the database.

Comments are closed.