Administrators Nathan Posted May 29, 2012 Administrators Share Posted May 29, 2012 Is it important to close your database connection at the end of your PHP file? I assumed it closed as soon as the page was done executing, so what would be the benefit in including it in your code? Quote Link to comment Share on other sites More sharing options...
__Darknite Posted May 29, 2012 Share Posted May 29, 2012 (edited) There are two schools of thought: * Open a connection and then close it immediately when you are done with it. * Open a connection and keep it open as long as you can. Now they both have pros and cons. I'm biased towards the first option, a database connection is a resource, in a highly transaction environment like a web server. It is best to use those resources in the most efficient manner, opening a connection and not closing it when not using it is like leaving a tap running! Edited May 29, 2012 by __Darknite Microsuck 1 Quote Link to comment Share on other sites More sharing options...
Microsuck Posted May 30, 2012 Share Posted May 30, 2012 If I am writing a quick script as a utility or something that I know itself won't take up a ton of resources, I won't close connections, but otherwise, I'll throw a mysql_close() in there. Quote Link to comment Share on other sites More sharing options...
bryce12 Posted June 9, 2012 Share Posted June 9, 2012 I can see several benefits of closing database connections: 1) It is considered as a good programming practice 2) It frees up resources that can be used by other processes 3) I'm not sure about PHP but in some languages open connection can be exploited by hackers so closing the connections would make our code more secure. Quote Link to comment Share on other sites More sharing options...
Marc Posted June 13, 2012 Share Posted June 13, 2012 I can see several benefits of closing database connections: 1) It is considered as a good programming practice 2) It frees up resources that can be used by other processes 3) I'm not sure about PHP but in some languages open connection can be exploited by hackers so closing the connections would make our code more secure. Actually its only good programming practice provided you are not going to reopen the connection within seconds of closing it down. Otherwise you are actually wasting resources by closing it down. bryce12 1 Quote Link to comment Share on other sites More sharing options...
JHTech100 Posted June 15, 2012 Share Posted June 15, 2012 I never have explicitly closed connections, to be honest. I basically just let the processing finish, but then again, a lot of stuff I work on calls to a database very frequently. Quote Link to comment Share on other sites More sharing options...
ALGirl Posted January 12, 2013 Share Posted January 12, 2013 I have read that when the page is finished loading (i.e. the script ends) the connection closes anyway. So, unless the page is very intense, it would make sense to not worry too much about it. Quote Link to comment Share on other sites More sharing options...
Administrators Nathan Posted January 12, 2013 Author Administrators Share Posted January 12, 2013 I have read that when the page is finished loading (i.e. the script ends) the connection closes anyway. So, unless the page is very intense, it would make sense to not worry too much about it. It's a good security measure. Quote Link to comment Share on other sites More sharing options...
Marc Posted January 12, 2013 Share Posted January 12, 2013 I have read that when the page is finished loading (i.e. the script ends) the connection closes anyway. So, unless the page is very intense, it would make sense to not worry too much about it. Not every database is on a webpage Quote Link to comment Share on other sites More sharing options...
simplysidy Posted January 14, 2013 Share Posted January 14, 2013 (edited) Adding a mysql_close() is not that big a task and neither it is resource centric - at least on my part as a coder . This is one reason, I do add it at the end of the code when I am sure the rest of the code will not need any access to the database. Though I have read on many many places that this is not required and PHP would close the connection (or rather drop the connection) automatically at the end, still, why not do it manually and rest in peace Edited January 14, 2013 by simplysidy Quote Link to comment Share on other sites More sharing options...
johnthomas1433 Posted January 15, 2013 Share Posted January 15, 2013 Yes. It shall close automatically when the page is finished executing. But when you are thinking about multiple databases and multiple tables, it does become relevant to close your connection as soon as you are finished with it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.