Is there any reason why I should close the connection after a query or at the end of the script?
What are the benefits of doing/no doing do?
See Question&Answers more detail:osIs there any reason why I should close the connection after a query or at the end of the script?
What are the benefits of doing/no doing do?
See Question&Answers more detail:osThe connection (if not persistent) is always closed at the end of the script, so, in theory, you don't need to close it yourself.
Still, if your PHP script takes lots of time to execute, it's a good idea to close the connection when you don't have to do any request to the database anymore -- at least, if the long calculations are done after the queries.
This is especially true if your application is deployed on a shared hosting : your user account can generally only have a few connections opened at the same time. (That number of simultaneous opened connections can be pretty small on shared hosting ; it's generally bigger on private servers).
The reason we often don't close connections ourselfves is :