Welcome Guest, Not a member yet? Register   Sign In
Only connect to database if there is a query to run
#1

[eluser]Unknown[/eluser]
At the moment I load the database class in my autoload which automatically creates a connection to mysql. If there are no queries it will have still created a connection.

For performance I only want to connect to mysql if a query has been run.

What would be the best way to achieve this?

I am thinking of writing a model function that all queries run through which detects if the database has been connected to or not, and simply calls $this->load->database() if not before running $this->db->query().

The problem with this is that I would have to change all of my $this->db->query() references in my code which is a pain.

Ideally I would like to extend the $this->db->query() function to support this.

Any ideas?

* edit: didn't see the autoinit function ;-)
#2

[eluser]gullah[/eluser]
I've been loading the database in the constructor of my models lately. It seems to be working fine and it forces me to use my models instead of being lazy and calling the database from my controller.




Theme © iAndrew 2016 - Forum software by © MyBB