Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
)
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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;
}
share|improve this answer
    
can you please let me know how can I close that new db connection? –  Muhammad Sajid Jun 20 '13 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. –  user1777018 Jun 20 '13 at 10:32
add comment

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

share|improve this answer
    
then who could I access new db tables & query the records & then close connection? –  Muhammad Sajid Jun 20 '13 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 '13 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 ... –  Muhammad Sajid Jun 20 '13 at 8:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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