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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.