Not sure if this is a php or mysql question but since it originates in php, I’ll ask it here. I have a database connection file and I use an include throughout the program in different places to establish a db connection. My question regards how those connections close and how long they remain open and what is the consequence of that if any?
First I always use include rather than include_once, does that matter.
And secondly, I never close them. Is there a way to do that and should I be doing that?
The connections one established remain till end of the script (included and direct) if it is not closed manually by mysql_close() function. So once you establish the connection you don’t have to connect again if you haven’t closed it. Since the manual itself says
Opens or reuses a connection to a MySQL server.
It reuses the connection if it is already connected to the server. I hope you can find the use (differences) of include_once and include functions. There is no any bad consequence by unclosed connections as far as I know.
Unless you expect your script to run for more than 1/10th of a second or so (in other words - a really long time) then you don’t really need to worry about closing the connection as it will close automatically when the script ends.
It is only where you have long running scripts where most of the processing occurs after you have finished with the database that you would benefit from closing it.