CodeIgniter Forums
Database Connection - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Database Connection (/showthread.php?tid=38529)



Database Connection - El Forum - 02-10-2011

[eluser]stewartf[/eluser]
Wondering how to connect to a database within a custom library. I attempted to use $this->load->database() which returned this : Undefined property: Cataloglib::$load.

So the question is, what did I do wrong?


Database Connection - El Forum - 02-10-2011

[eluser]stewartf[/eluser]
I added ' extends CI_Controller ' to the class and that worked. To take it a step further, is this a good practice?


Database Connection - El Forum - 02-10-2011

[eluser]cideveloper[/eluser]
I do it this way

Code:
public function __construct() {
    $this->CI =& get_instance();
}

public any_other_function(){
    $this->CI->load->database();
}


This is the way I have seen it done in most other big project using CI


Database Connection - El Forum - 02-10-2011

[eluser]stewartf[/eluser]
Very nice. Thank you.


Database Connection - El Forum - 02-10-2011

[eluser]danmontgomery[/eluser]
(Libraries shouldn't extend the controller)