Welcome Guest, Not a member yet? Register   Sign In
CI2 - multiple database bug
#1

[eluser]ripken204[/eluser]
This person is having the same issue as me, so read it as well:
http://www.phpfreaks.com/forums/applicat...deigniter/

I am trying to connect to databases in the constructor of models.
My two models are below.
The Account class you will notice has print_r($this->DB2) right before I perform the query, this shows the correct information.
The odd thing is that it throws the error

Quote:Table 'teams2010.accounts' doesn't exist
SELECT * FROM accounts WHERE id='1'
Filename: application/models/account.php

This is the database name for my Teams class, im not sure how it even knows about that, especially since the line before where i print out the DB info, it says the database is the Users database.

In my controller, if i dont load the Teams model, it works fine.
If i load only my Teams model and not my Account model, it works fine.

So obviously something is messed up here, I am not sure what could be wrong with my code..
It's as if CI2 does not use the information that is in my database resource, but instead is using the last connected database's information.

This is for accessing user accounts
Code:
class Account extends CI_Model {

    // The table storing user accounts
    var $table = 'accounts';
    private $DB2 = NULL;

    function __construct() {
        parent::__construct();
        $this->DB2 = $this->load->database('admin', TRUE);
    }
    

    function get($id) {
        echo "<br><br><br>";
        print_r($this->DB2);
        echo "<br><br><br>";

        $query = $this->DB2->query("SELECT * FROM $this->table WHERE id='$id'");
        
        if ($query->num_rows() === 1) {
            return $query->row();
        }
        
        return NULL;
    }
}


This does nothing yet
Code:
class Teams extends CI_Model {

    private $DB1;

    function __construct() {
        parent::__construct();
        $this->DB1 = $this->load->database('teams', TRUE);
    }
}
#2

[eluser]ripken204[/eluser]
ok so i noticed something else..

i changed this to false in my databases.php file
$db['default']['autoinit'] = FALSE;

this fixed the error...
until i decide to add something simple to my Teams model

with the query in the getAll function, i receive the same error in my previous post, if i comment it out i receive no errors.
so somehow loading the database in this model affecting the connection in my Account model

Code:
class Teams extends CI_Model {

    private $DB1;

    function __construct() {
        parent::__construct();
        $this->DB1 = $this->load->database('teams', TRUE);
    }

    function getAll($orderby = 'school') {
        
        $query = $this->DB1->query("SELECT * FROM coaches ORDER BY $orderby");
    
    }    
}
#3

[eluser]ripken204[/eluser]
and i figured it out...
http://codeigniter.com/wiki/Modularity_i...iter_PHP5/

# new way
$query1 = DB('db1')->query($sql);
$query2 = DB('db2')->query($sql);

the user guide does not show this.. at least i could not find it
#4

[eluser]CroNiX[/eluser]
http://ellislab.com/codeigniter/user-gui...cting.html
#5

[eluser]ripken204[/eluser]
i am fully aware of that..
if you look at my code in the first post it follows that method of connecting to multiple databases, unless you see something wrong that i did.
#6

[eluser]spaquet[/eluser]
Yep
Code:
if ($query->num_rows() === 1) {
should be
Code:
if ($query->num_rows() == 1) {
there is a = in spare...




Theme © iAndrew 2016 - Forum software by © MyBB