Jump to content

connect to mysql db function


mds1256

Recommended Posts

I have created a function to connect to a required database with a username and password.

 

I have called this db_openConnection()

 

Now for each function that requires a select from a database is it ok just to call the db_OpenConnection() first within the function or should i be keeping open a db connection (using persist, i think) rather than keep creating a connection.

 

I know that the connection ends when you have finished a query any how.

 

Not sure if this is the correct forum or if it should be mysql forum

Link to comment
Share on other sites

I know that the connection ends when you have finished a query any how.

 

No it doesn't. Where did you get that information from?

 

A connection remains open until you specifically close it or until it is automatically closed when the script execution ends on the current page request.

Link to comment
Share on other sites

I know that the connection ends when you have finished a query any how.

 

No, the connection ends when the script finishes. You should connect to the database once, then run all needed queries. Edit: PFMaBiSmAd beat me to that response.

 

Rather than a function, you could create a db class. Then just have a method in the class to run a function. When that method is called the class will check if a db conneciton is already open. If so, it runs the query. If not, it opens the connection before running the query. That way a db connection is only opened if needed and you only need to open it once per page.

 

You could do the same thing with a function, but that would require using a global variable for the db connection. But, there are many db classes freely available - no need to reinvent the wheel.

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.