WordPress.org

Ready to get started?Download WordPress

Forums

Access to second database from php include breaks WP queries after. (5 posts)

  1. DigitaLink
    Member
    Posted 4 years ago #

    Hoping there's a guru here who can give me a quick and simple answer.

    I'm working on some custom page templates, which involves accessing a pre-existing database that is NOT part of my WP install, to retrieve data back into my WP theme. Basically I include a config file that connects to the NON-WP database, run some queries, output the info, and carry on with the rest of the page via WP functions.

    And while my included queries work just fine, PHP is now connected to a different database, and stops loading up the rest of the WP content. If I do this in the theme header, it takes out my custom menu, formatting for things like galleries, etc.

    How do I tell WP to re-connect to the WP database after I'm done in the other one? I took a look through the wpdb function reference, but there is no mention in there of how to connect to another db, just that you should instantiate your own object from the wpdb class.

    I'm sure it's probably something very simple, for someone who already knows what they're looking for.

  2. New Nine Media
    Member
    Posted 4 years ago #

    I'm sure someone else can offer more, but I remember reading somewhere that you can't connect to a different database and then switch back. I refuse to believe that, but I can only image that you'd likely have to grab the query, close the WP connection, open your own connection, run your query on Table2, close the connection, and then reopen the WP query and rerun it from scratch.

    Could you move the tables from the other database to your WordPress database or is that not an option? This would be significantly easier and would probably cause less server overhead.

  3. DigitaLink
    Member
    Posted 4 years ago #

    HAHA!! I found my answer in wp-settings.php. After I'm done with the 2nd database, I just have to add:
    wp_set_wpdb_vars();
    as the next line, and everything works as it should again. That was amazingly easy ... once I delved through enough code to find the magic little line to make things work. I said it was probably something very simple ... :)

    ------- UPDATE -------------
    Hmmm ... on second glance, it's not ALL working quite right with that fix. Custom menus and styling seem to be working, but the calendar widget still isn't showing posts on dates that have them. But I suppose I can live with that if I'm not using the calendar widget. ;)

  4. Darrell Schauss
    Member
    Posted 4 years ago #

    Are you including a config file with

    define('DB_NAME', '');
    define('DB_USER', '');

    ..etc? This will overwrite your base WordPress installs wp-config.php settings when you load up your own.

    You could do like this.

    define('DB2_NAME', '');
    define('DB2_USER', '');

    etc..

    And you can connect to another database

    $databasetwo = mysql_connect('DB2_HOST','DB2_USER','DB2_PASSWORD');
    mysql_select_db('DB2_NAME',$databasetwo);
    $results = mysql_query("SELECT...",$databasetwo);
    //do stuff...
    mysql_close($databasetwo);

    I am doing this in the middle of my index.php without having to do anything with the current WordPress connection.

  5. DigitaLink
    Member
    Posted 4 years ago #

    Nope, wasn't over-writing any defined values ... but in the end I decided it wasn't worth fighting with for the sake of experimenting.

    I exported my database, imported it to the pre-existing database, changed wp-config, and now it's all happy as can be. Some days I swear I just try to make things harder for myself. LOL I'm going to bookmark your solution though - it looks like a good workaround.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.