CodeIgniter Forums

Full Version: Database Connection
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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?

El Forum

[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?

El Forum

[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

El Forum

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

El Forum

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