SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow 2nd DB Connection in WordPress

    Hi Guys,

    I wrote a custom WordPress plugin that needs access to another database in MySQL (seperate from the wordpress DB, in other words).

    I include my own, non-wp, mysqli_connect.php file in my wordpress installation which simply does the following:

    PHP Code:
    DEFINE ('DB_USER''XXXX');
    DEFINE ('DB_PASSWORD''XXXXX');
    DEFINE ('DB_NAME''outside_db');
    DEFINE ('DB_HOST''localhost');
     
    // Instantiate MySQL connection object:
    $GLOBALS['dbc'] = new mysqli(DB_HOSTDB_USERDB_PASSWORDDB_NAME);
     
    if (
    mysqli_connect_errno())
    {
        throw new 
    RuntimeException('Cannot access database: ' mysqli_connect_error());

    But when I try to access this database using $GLOBALS['dbc'] in my wordpress plugins (ie. trying to run queries), it doesn't work and I'm not getting any error messages from mysql.

    The mysqli_connect.php file above is bullet-proof since I use it for my own non-wordpress scripts with success, but I'm assuming the problem lies in the fact that there are two mysql database connections going on inside of my wordpress.

    Again, I'm trying to make this second DB connection within a wordpress.

    I really have never dealt with two database connections at the same time, so I don't know if there is some conflict going on I don't know how to handle.

    Does anyone have any hints or guesses as to what is going wrong here and why I can't use this second database connection? I know I can use WordPress' built-in $wpdb class to run queries on a second database (and I've done this with success), but I have so many other classes/functions that use mysqli for database stuff that I don't want to rewrite everything.

    Anyone got any ideas? Thanks so much for any and all help and guidance.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aren't those names the same as the wordpress ones, try OTHER_DB_NAME etc

  3. #3
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,000
    Mentioned
    101 Post(s)
    Tagged
    0 Thread(s)
    If your using OOP then just have two instances of the your DB class, making sure that each gets the db login info it needs.

    If using procedural then wrap the db connection up into a function, make two versions and (one with db login info for word press and one for whatever other stuff). You would then call the appropriate db connection function.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  4. #4
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume your code isn't that bullet-proof. Since you are not getting any error messages from mysql.
    Does anyone have any hints
    I'd start from getting any error messages from mysql.

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    Aren't those names the same as the wordpress ones, try OTHER_DB_NAME etc
    hash.. YOU ARE THE MAN!!!!

    I didn't even realize that I was trying to define constants that wordpress may have already defined. Holy crap... this is awesome. I was so clueless as to why all this wasn't working.

    I changed my constant defs and everything is WORKING!!! Yeee haw!!

    Thank you hash!!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •