Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if MySQL connections continually aren't closed on PHP pages?

Tags:

php

mysql

At the beginning of each PHP page I open up the connection to MySQL, use it throughout the page and close it at the end of the page. However, I often redirect in the middle of the page to another page and so in those cases the connection does not be closed. I understand that this is not bad for performance of the web server since PHP automatically closes all MySQL connections at the end of each page anyway. Are there any other issues here to keep in mind, or is it really true that you don't have to worry about closing your database connections in PHP?

$mysqli = new mysqli("localhost", "root", "", "test");
...do stuff, perhaps redirect to another page...
$mysqli->close();
like image 283
Edward Tanguay Avatar asked Sep 19 '08 16:09

Edward Tanguay


1 Answers

From: http://us3.php.net/manual/en/mysqli.close.php

"Open connections (and similar resources) are automatically destroyed at the end of script execution. However, you should still close or free all connections, result sets and statement handles as soon as they are no longer required. This will help return resources to PHP and MySQL faster."

like image 67
David McLaughlin Avatar answered Oct 28 '22 10:10

David McLaughlin