WordPress choose wrong database

I have 2 databases. When I include the database connection (with a include('filename.php');) wordpress acts really strange.

I can’t login, the wordpress username and password is right but it tries to connect to the other database (which I can see when I turn debug on).

Read More
WordPress database error: [Table 'api.apiwp_options' doesn't exist]
SELECT option_value FROM apiwp_options WHERE option_name = 'widget_pages' LIMIT 1
WordPress database error: [Table 'api.apiwp_options' doesn't exist]
SELECT option_value FROM apiwp_options WHERE option_name = 'widget_calendar' LIMIT 1
WordPress database error: [Table 'api.apiwp_options' doesn't exist]
SELECT option_value FROM apiwp_options WHERE option_name = 'widget_links' LIMIT 1
WordPress database error: [Table 'api.apiwp_options' doesn't exist]
SELECT option_value FROM apiwp_options WHERE option_name = 'widget_tag_cloud' LIMIT 1
WordPress database error: [Table 'api.apiwp_options' doesn't exist]
SELECT option_value FROM apiwp_options WHERE option_name = 'widget_nav_menu' LIMIT 1

It’s like they can not separate the connections. Anything that can help me in the right direction?

The content also stops working.

Edit: Clearifying

My Database connection:

function ir_mysql() {
    global $dbm;

    if(empty($dbm)){
        $dbm = new Mysql(DBHOST, DBUSER, DBPASS, DBNAME);
    }
    return $dbm;
}

mysql.class.php

class Mysql {

    // Variabler 
    // -----------------------------
    private $db;
    private $user;
    private $password;
    private $host;
    private $linkid;
    private $result;

function __construct($host, $user, $password, $db) {
        try {
            $this->db = $db;
            $this->user = $user;
            $this->password = $password;
            $this->host = $host;

            $this->connect();
            $this->select();
            mysql_set_charset('utf8', $this->linkid);
        } catch (exception $e) {
            die($e->getMessage());
        }
    }


    function Connect() {
        try {
            $this->linkid = mysql_connect($this->host, $this->user, $this->password);
            if (!$this->linkid) {
                throw new Exception(mysql_error());
            }
        } catch (Exception $e) {
            die($e->getMessage());
        }
    }


    function Select() {
        try {
            if (!mysql_select_db($this->db, $this->linkid)) {
                throw new Exception("Cannot access the selected database");
            }
        } catch (Exception $e) {
            die($e->getMessage());
        }
    }


    function Query($sql) {
        try {
            $this->result = @mysql_query($sql, $this->linkid);

            if (!$this->result) {

                throw new Exception("Mysql query error: n <br />" . mysql_error() . "<br />" . mysql_errno());
            }
        } catch (Exception $e) {
            if (ir_admin()) {
                die($e->getMessage());
            }
        }

        return $this->result;
    }

function Close() {
        mysql_close($this->linkid);
}

}

So if I’m querying something new I do like this: ir_mysql()->query("SELECT....");

My method and wordpress method are not playing well.

Related posts

Leave a Reply

1 comment

  1. In case someone is interested. I made it work.

    I made a plugin and in the main php-file I wrote this.

    <?php
    
    function ir_load(){
        include_once(ABSPATH . 'load.php');
    }
    
    function db_fix_login(){
        global $wpdb;
        $wpdb->select(DB_NAME);
    }
    
    add_action("wp_head","db_fix_login", 0);
    
    ir_load();
    db_fix_login();
    
    ?>
    

    The ir_load() contains the database connection to my database. I made it a function in case I needed to change or include it somewhere else.

    db_fix_login needed to go to wp_head() (since I use get_header() on my subpage to keep wordpress functionality).

    Everything worked except that I got logged out all the time if I only runned ir_load().

    So after trying everything: Why not run db_fix_login() right after? It worked. No errors or anything!