• dnxpert

    (@dnxpert)


    Hi,

    I am wondering if I will break something if I do the following inside my theme’s index.php?

    global $wpdb;
    $wpdb = new wpdb(‘myuser’, ‘mypass’, ‘otherdb’, ‘myserver’);

    do stuff…

    assign back to original
    global $wpdb;
    $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

    I want to do this to get some info from another wordpress that I run on the same server.

    Thank you for an answer.

Viewing 7 replies - 1 through 7 (of 7 total)
  • ambrosite

    (@ambrosite)

    There was some discussion about accessing a second database from WP here:

    http://wordpress.org/support/topic/307945?replies=8#post-1201613

    Don’t know if that will help you, but it’s worth a look.

    Thread Starter dnxpert

    (@dnxpert)

    Thanks ambrosite, already saw that link but not exactly what i am looking for.

    Did run into the hyperdb plugin which is more like what I need.

    Thread Starter dnxpert

    (@dnxpert)

    For anyone interested, here is how I solved the problem:

    1. call first db db1
    2. call second db db2
    3. have table prefix on first db set to prefix1_
    4. have table prefix on second db set to prefix2_
    5. installed hyperdb plugin from:
    http://wordpress.org/extend/plugins/hyperdb/
    (followed instructions in readme file).

    6. configured hyperdb db-settings.php file to read from the 2 dbs by adding the two databases via add_db_server();
    add_db_server(‘global’, 0, ”, 1, 1, DB_HOST, DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);
    add_db_server(‘db2’, 0, ”, 1, 1, DB_HOST, DB_HOST, ‘db2’, DB_USER, DB_PASSWORD);

    7. Also inside db-settings.php of hyperdb I told hyperdb to query the second database for all tables belong to that database using a call to add_db_table (i added all tables via the method call like so):
    add_db_table(‘db2’, ‘prefix2_commentmeta’);
    add_db_table(‘db2’, ‘prefix2_comments’);
    add_db_table(‘db2’, ‘prefix2_links’);
    add_db_table(‘db2’, ‘prefix2_options’);
    add_db_table(‘db2’, ‘prefix2_postmeta’);
    add_db_table(‘db2’, ‘prefix2_posts’);
    add_db_table(‘db2’, ‘prefix2_terms’);
    add_db_table(‘db2’, ‘prefix2_term_relationships’);
    add_db_table(‘db2’, ‘prefix2_term_taxonomy’);
    add_db_table(‘db2’, ‘prefix2_usermeta’);
    add_db_table(‘db2’, ‘prefix2_users’);

    6. finally, inside the code where I wanted to read second db posts on first site, I did the following:

    global $wpdb;
    $wpdb->set_prefix(‘prefix2_’);

    do all my processing…
    then change prefix back to original inside wpdb object

    $wpdb->set_prefix(‘prefix1_’);

    Works like a charm!

    ambrosite

    (@ambrosite)

    Thanks very much for sharing your solution.

    Thread Starter dnxpert

    (@dnxpert)

    No probs, I hope someone can find it useful.

    Nice one 🙂

    Its give the following error…

    Fatal error: Call to undefined function add_db_server() in D:\wamp\www\softaula\db-config.php on line 315

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Can I reassign global $wpdb object in a theme?’ is closed to new replies.