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

[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
#2

[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
#3

[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
#4

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

[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
#6

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

[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