Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I $mysqli->close a connection after each page load, if PHP runs via FCGI?

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?

like image 952
bodacydo Avatar asked Oct 10 '14 01:10

bodacydo


People also ask

Do I need to close Mysqli connection?

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.

How can I close database connection in PHP?

To close the connection in mysql database we use php function conn->close() which disconnect from database. Syntax: conn->close();

Which of the following function command in PHP is used to connect MySQL database?

The connect() / mysqli_connect() function opens a new connection to the MySQL server.

What does Mysqli<UNK>close do?

The close() / mysqli_close() function closes a previously opened database connection.


1 Answers

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.

like image 91
Colin Schoen Avatar answered Dec 09 '22 02:12

Colin Schoen