CodeIgniter Forums
Question about multiple databases - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Question about multiple databases (/showthread.php?tid=12276)



Question about multiple databases - El Forum - 10-13-2008

[eluser]St0neyx[/eluser]
hi all

i'm building a project where i'm using 2 databases, both are on the same server as the website.
now i read a lot of topics and the userguide, and what i find is this.

Code:
$DB1 = $this->load->darabase('dbname 1', true);
$DB2 = $this->load->darabase('dbname 2', true);

now i tryd this, but it isn't working as i hoped..
Im using a lot of DHTML and ajax in my project, wich means that i use a lot of calls to functions.

now when i call a function that needs to gether data from both databases i did this:

Code:
$DB1 = $this->load->darabase('dbname 1', true);
$DB2 = $this->load->darabase('dbname 2', true);

$query1 = "SELECT * FROM bla";
$query2 = "SELECT * FROM blaat";

$result1 = $DB1->query($query1);
$result2 = $DB2->query($query2);

$data['result1'] = $result1;
$data['result2'] = $result2;

return $data;

This will not work because when the first query is send, it usses the second db connection.
Kinda wierd because i put them in different variables.

To make it work i have to do this (in every function)

Code:
$DB1          = $this->load->darabase('dbname 1', true);
$query1      = "SELECT * FROM bla";
$result1      = $DB1->query($query1);
$data['result1'] = $result1;

// not nessessery, but do it anyhow
$DB1->close();

$DB2          = $this->load->darabase('dbname 2', true);
$query2      = "SELECT * FROM blaat";
$result2      = $DB2->query($query2);
$data['result2'] = $result2;

So my solution is, i made a model (db_model):

Code:
function query($query, $db)
{
    switch ($db)
    {
        case 'db1':
            // select db
            $DB1 = $this->load->database('db1', TRUE);
                
            // send query
            $result = $DB1->query($query);
                
            // close the db
            $DB1->close();
                
            // return the resultset
            return $result;
            break;
        case 'db2':
            // select the right db
            $DB2 = $this->load->database('db2', TRUE);
                
            // send query
            $result = $DB2->query($query);
                
            // close the db
            $DB2->close();
                
            // return the resultset
            return $result;
            break;
        default:
            // generate error
            break;
    }
}

Call from function:

Code:
$query1 = "SELECT * FROM bla";
$query2 = "SELECT * FROM blaat";

$data['result1'] = $this->db_model->query($query1, 'db1');
$data['result2'] = $this->db_model->query($query2, 'db2');

return $data

This will work, but im not sure if this is the way to fix it.
Can i get some feedback on this??

Kind regards


Question about multiple databases - El Forum - 10-13-2008

[eluser]xwero[/eluser]
I think you have to set the pconnect setting to false if you want to use 2 databases at the same time.


Question about multiple databases - El Forum - 10-13-2008

[eluser]St0neyx[/eluser]
[quote author="xwero" date="1223910130"]I think you have to set the pconnect setting to false if you want to use 2 databases at the same time.[/quote]

I saw that pconnect is one of the major causes for this, but then again if i set it to false i still have to make all the steps for a query
1. select the right db
2. make query
3. send query
4. close db
5. do something with result

Can't i just define both dbs and then set pconnect to false so the only steps are
1. make query
2. send query
3. do something with result

i cant put them in the construct because that one is not called.