Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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).

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.

share|improve this question
7  
I don't follow what "include the database connection" means. What exactly is happening in that file? –  Rarst Oct 2 '12 at 12:11
3  
Please use the $wpdb class. You can read more about it in the Codex. –  kaiser Oct 2 '12 at 12:28

1 Answer 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!

share|improve this answer
    
Just curious what a non-db database is... –  Brian Fegter Oct 3 '12 at 9:09
    
Sorry, my mistake. Gone now. –  jocken Oct 3 '12 at 11:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.