Welcome Guest, Not a member yet? Register   Sign In
multiple db connection issue

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
function MGalleries()
    $db = $this->load->database('default', TRUE);
    $check = $this->load->database('mine', TRUE);

Then, later, I try to use one of them using

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,


It's because you decalre the variable in the constructor in the constructor, so it's only visible there
class MGalleries extends Model {
    private $db;
    private $check;
    public function __construct() {
        $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

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

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

[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

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!!!

[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

Theme © iAndrew 2016 - Forum software by © MyBB