Multiple Database Question

#1
[eluser]louis w[/eluser]
I have two databases set in my database config. One as ['default'], which is the one I will primarily be using. On a rare occasion I will need to connect to another one.

Inside my model I perform my normal db queries (on the default database) and then if I need to connect to the other one I load it this way:
$db_teaser = $this->CI->load->database('db_teaser', TRUE);

However the problem arises that if later on in my model I want to query the default database again (e.g. $this->db->simple_query), it doesn't work. It's trying to connect to the second database loaded.

Is there a way for $this->db to always query the default database and the second one to work with $db_teaser?

Problem with loading the default database not as $this->db is that when I dont have two databases loaded I would still have to reference it like this.

Thanks. Hope this is clear.

#2
[eluser]louis w[/eluser]
As a followup question is it possible to load a second database, do something and then unload it? That might also accomplish what I am hoping to do.

#3
[eluser]mania[/eluser]
did you figure it out?

did you try calling a separate model for each db?

#4
[eluser]mania[/eluser]
here is perhaps the answer to our problem.

http://ellislab.com/forums/viewthread/95492/

#5
[eluser]Tom Schlick[/eluser]
var $db_teaser
^^ that goes outside of the function so you can use it in the whole class.

$this->db_teaser = $this->CI->load->database(‘db_teaser’, TRUE);
^^ that goes in the contructor

$this->db->query();
$this->db_teaser->query();
^^ and your queries

#6
[eluser]louis w[/eluser]
I never got it to work with two databases in the same model. I moved the second db call out to the controller.

trs21219, that is pretty much what I tried except I was not storing my $db_teaser in the controller. But does this work for you?

Code:
$this->db->query();
$this->db_teaser->query();
$this->db->query();

The problem I was having was using the $this->db after the $db_teaser. Because $db had turned into the connection to db_teaser.

I will give it another shot this afternoon however and see if i have any luck. I will let you guys know.

#7
[eluser]mania[/eluser]
[quote author="louis w" date="1233089367"]

The problem I was having was using the $this->db after the $db_teaser. Because $db had turned into the connection to db_teaser.[/quote]

If you read that thread I linked to all you have to do is use a different mysql username for your other database. this is good practice anyway.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.