Is closing the mysql connection important?

From the documentation:

Note: The link to the server will be closed as soon as the execution of the script ends, unless it’s closed earlier by explicitly calling mysql_close().

If your script has a fair amount of processing to perform after fetching the result and has retrieved the full result set, you definitely should close the connection. If you don’t, there’s a chance the MySQL server will reach it’s connection limit when the web server is under heavy usage. If you can’t close the MySQL connection until near the end of the script, it’s cleaner though unnecessary to do so explicitly.

I’m not certain how fastcgi affects things. One page claims that a build of PHP that supports fastcgi will create persistent connections, even for mysql_connect. This contradicts the documentation in that the connection is closed when the process, rather than the script, ends. Rather than testing it, I’m going to recommend using mysql_close(). Actually, I recommend using PDO, if it’s available.

Leave a Comment