[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