CodeIgniter Forums
$this->load->database() in every control function - 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: $this->load->database() in every control function (/showthread.php?tid=15801)



$this->load->database() in every control function - El Forum - 02-15-2009

[eluser]gnomixa[/eluser]
Is there a way to add it at the beginning of the application once? Calling this at the beginning of every function in the control file seems like a bad habit....also, does this create a new database connection? if so, when does it get closed?

i am concerned about database optimization and don't want to create connection that just hang there.
Please enlighten the CI newbie!


$this->load->database() in every control function - El Forum - 02-15-2009

[eluser]pistolPete[/eluser]
Quote:Is there a way to add it at the beginning of the application once?
Just have a look at the user guide: Automatically Connecting

A database connection gets closed on script exit, but you can also force it quit, although there is no real need to do so:
Code:
$this->db->close();



$this->load->database() in every control function - El Forum - 02-15-2009

[eluser]fesweb[/eluser]
If you are always using the same database, you should just save your database info once in your config/database file, and then autoload the database library in your config/autoload file.

Code:
// in config/autoload
$autoload['libraries'] = array('database', 'session');



$this->load->database() in every control function - El Forum - 02-15-2009

[eluser]gnomixa[/eluser]
Thanks!