• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

Leave database connection open throughout or close everytime?

If I'm going to make 2 or more database connections in one php page, should I connect in the beginning of the file and close it at the end, or open and close for each occurrence?
0
dsrnu
Asked:
dsrnu
  • 2
  • 2
1 Solution
 
shobinsunCommented:
Hi,

You can use the multiple connections in anywhere in the page you wants.

No need to close the connection after each one.

Please note that you sholud connect with the different databases with different connection objects.

Regards
0
 
shobinsunCommented:
Hi,

 mysql_close() closes the non-persistent connection to the MySQL server that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is used.

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution
0
 
cdaugustinCommented:
Hi,

just to make it clear, when a php script finishes it closes the connections to the db that it opened (most of the time, there are exceptions).

The right thing to do is to open DB connections only when you need them AND reuse the connection variable for further queries. Declare the connection variable at the top of the script and when you need it first time connect to the db (maybe put the code a function for this). Usually there is an DB abstraction layer used to optimize this process (an abstraction layer in layman terms means a bunch of functions/classes that help you organize better the way you connect/use the db/db connections).

You should NEVER open multiple connections to the same database or put db open code into loops as this is poor coding (it costs you resources and execution time) which may lead to serious problems easily.

Your probably better off if you open the connections at the top of the script and let PHP close them at the end of the script (this is unless you know for sure you wont need your connection after some point in the script and that your script will take a long time to run). MySQL has a maximum number of connections so you need to take this in consideration when you are using the db on scripts that run often or take a long time to finish execution.
0
 
cdaugustinCommented:
to resume and straight answer your question: dont open&close each time you do a query, open once and reuse. This changes if you know that the time between queries in the script is separated by minutes of processing (more than 3mins) in which case depending on your load on the server you might want to free up the mysql connection (this is in relation with that max number of connections on the SQL server).
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now