Part of the EllisLab Network
   
 
Question about multiple databases
Posted: 13 October 2008 03:57 AM   [ Ignore ]  
Summer Student
Total Posts:  17
Joined  03-26-2007

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.

$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:

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

$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):

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:

$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

Profile
 
 
Posted: 13 October 2008 04:02 AM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

I think you have to set the pconnect setting to false if you want to use 2 databases at the same time.

Profile
 
 
Posted: 13 October 2008 04:11 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  17
Joined  03-26-2007
xwero - 13 October 2008 04:02 AM

I think you have to set the pconnect setting to false if you want to use 2 databases at the same time.

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.

Profile
 
 
   
 
 
‹‹ formatting query results      Remapping ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 10:15 AM
Total Registered Members: 119534 Total Logged-in Users: 50
Total Topics: 125766 Total Anonymous Users: 5
Total Replies: 661786 Total Guests: 492
Total Posts: 787552    
Members ( View Memberlist )