Different Database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Different Database (/showthread.php?tid=13447) |
Different Database - El Forum - 11-22-2008 [eluser]eiso[/eluser] I am looking to use/connect to a different database for one of my controllers and one model. What is the least unobtrusive way to do this, I don't want to have to change any of my other models or controllers by using the $DB1 & $DB2 s described in the user guide. Thank you very much, Eiso Different Database - El Forum - 11-23-2008 [eluser]Randy Casburn[/eluser] Hi eiso, Welcome to CI! 1) Make sure your second database group is declared with a non-persistent connection type 2) $secondDB = $this->load->database('groupname',TRUE); 3) $secondDB->query('DELETE * FROM USERS'); // JUST kidding with that last statement ;-) You only need to worry about the naming convention with the second DB. It will keep you out of trouble down the road if you do both. For a retro-fit this will work fine. Hope this helps, Randy Different Database - El Forum - 11-23-2008 [eluser]eiso[/eluser] I added this in database.php: Code: $db['tdb']['hostname'] = "localhost";//localhost This as my model: Code: <?php And this is the start of my controller: Code: <?php And I get this error: A PHP Error was encountered Severity: Notice Message: Undefined variable: tdb Filename: models/tadmin_model.php Line Number: ... Fatal error: Call to a member function query() on a non-object in /blablabla/tadmin_model.php on line ... What am I doing wrong here? Thank you, Eiso p.s. changed some some lines for security purposes Different Database - El Forum - 11-23-2008 [eluser]Randy Casburn[/eluser] Your member varible is scoped incorrectly. $this->tdb will provide you the object you seek. When you only refer to $tdb, you're only in the function's scope. You need to refer to the class' scope with the $this keyword. Randy Different Database - El Forum - 11-23-2008 [eluser]eiso[/eluser] Thank you Randy - I changed it and am now getting the following error: A PHP Error was encountered Severity: Notice Message: Undefined property: Tadmin_model::$tdb Filename: models/tadmin_model.php Line Number: ... Different Database - El Forum - 11-23-2008 [eluser]Randy Casburn[/eluser] Right...your assignment statement statement must include $this as well: Code: <?php Let's see if that fixes things up. Randy Different Database - El Forum - 11-23-2008 [eluser]eiso[/eluser] It does! Thank you so much Randy, your help really has been greatly appreciated. Thank you!! Eiso Different Database - El Forum - 11-23-2008 [eluser]Randy Casburn[/eluser] Your very welcome. Glad to help. Have fun with CI! Randy |