I run PHP via FCGI - that is my web server spawns several PHP processes and they keep running for like 10,000 requests until they get recycled.
My question is - if I've a $mysqli->connect
at the top of my PHP script, do I need to call $mysqli->close
in when I'm about to end running the script?
Since PHP processes are open for a long time, I'd image each $mysqli->connect
would leak 1 connection, because the process keeps running and no one closes the connection.
Am I right in my thinking or not? Should I call $mysqli->close
?
Explicitly closing open connections and freeing result sets is optional. However, it's a good idea to close the connection as soon as the script finishes performing all of its database operations, if it still has a lot of processing to do after getting the results.
To close the connection in mysql database we use php function conn->close() which disconnect from database. Syntax: conn->close();
The connect() / mysqli_connect() function opens a new connection to the MySQL server.
The close() / mysqli_close() function closes a previously opened database connection.
When PHP exits it closes the database connections gracefully.
The only reason to use the close method is when you want to terminate a database connection that you´ll not use anymore, and you have lots of things to do: Like processing and streaming the data, but if this is quick, you can forget about the close statement.
Putting it in the end of a script means redundancy, no performance or memory gain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With