CodeIgniter Forums
multiple db connection issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: multiple db connection issue (/thread-19474.html)



multiple db connection issue - El Forum - 06-08-2009

[eluser]theprodigy[/eluser]
OK, this may be more than one subject, but here goes.....

I am building a module using Matchbox. I need to connect to multiple databases simultaneously. In my model constructor I have
Code:
function MGalleries()
{
    parent::Model();
    $db = $this->load->database('default', TRUE);
    $check = $this->load->database('mine', TRUE);
}

Then, later, I try to use one of them using
Code:
$db->select('...');

but I am receiving an error

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: db

Filename: models/mgalleries.php

Line Number: 12

What am I doing wrong? Is this something wrong with my Matchbox, or am I supposed to call the database loading from a controller and not in the model constructor?

Please help,

Thanks


multiple db connection issue - El Forum - 06-09-2009

[eluser]Dam1an[/eluser]
It's because you decalre the variable in the constructor in the constructor, so it's only visible there
Try
Code:
class MGalleries extends Model {
    private $db;
    private $check;
    
    public function __construct() {
        parent::Model();
        $this->db = $this->load->database('default', TRUE);
        $this->check = $this->load->database('mine', TRUE);
    }
}

You will now need to use $this->db and $this->check in the other functions to use it


multiple db connection issue - El Forum - 06-09-2009

[eluser]theprodigy[/eluser]
well, damn, I feel like an idiot now. I can't believe I let something like scoping get one over on me.

Oh well.

Thanks, Dam1an


multiple db connection issue - El Forum - 06-09-2009

[eluser]Dam1an[/eluser]
No worries, we've all been there (me too Sad)
At least you'll know what to look for next time


multiple db connection issue - El Forum - 06-09-2009

[eluser]TheFuzzy0ne[/eluser]
[quote author="Dam1an" date="1244567467"]At least you'll know what to look for next time[/quote]

Yeah, look for Dam1an, he'll help you. Wink


multiple db connection issue - El Forum - 06-09-2009

[eluser]theprodigy[/eluser]
heh, I'll be looking here on the forums. I've seen a lot of good knowledge here. A couple snide remarks here and there, but we all have bad days (trust me, this I know ;-) ).

I would like to say that ya'll are great, and thanks for taking the time to work with us. I love this community. You guys are the Bean-O ™ to my Brain-farts!!!


multiple db connection issue - El Forum - 07-26-2009

[eluser]Jedidiah Reeser[/eluser]
I made a video tutorial about connecting multiple databases in CI. I had a similar problem and this video shows the solution that I used.

Accessing Multiple Databases in CI Video Tutorial by Jedidiah Reeser