CodeIgniter Forums
Database Connection - 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: Database Connection (/thread-38529.html)



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)