Jump to content

Multiple queries, one db connection


Jonob

Recommended Posts

Hi all,

 

Just wanted a quick sanity check here, as I am not sure that I have this right.

 

Assume that I have a single class in a single php file, with multiple functions in the class. Lets say that one of these functions get executed, and it in turn calls a few other functions in the same class, and maybe a few functions in some other classes (files) too. Each function runs one or more queries against a mysql database.

 

Am I correct in thinking that I could just create a single database connection in the first class, and this will be used for all queries that get executed in the current scope? i.e. do something like a class constructor:

 

function __construct() {
  $connection = new db_connection();
  $connection->connect();
}

 

OK, so assuming that this is right, and that I have this constructor in all my classes, and I call Class B from Class A, then it will still connects twice, right? Or, is there a way to specify in the mysql connection function, that if the connection is active, then dont open it again.

 

Thanks for any advice you can give.

Link to comment
https://forums.phpfreaks.com/topic/181663-multiple-queries-one-db-connection/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.