• 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:

    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_HOST, DB_USER, DB_PASSWORD, DB_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.

  • The topic ‘Simple Question RE: 2nd DB Connection’ is closed to new replies.