2

Something I have always wondered, it seems that virtually no PHP applications ever explicitly close MySQL connections mysqli_close().

How does this work? Does PHP automagically close MySQL connections when scripts stop executing?

Justin
  • 42,716
  • 77
  • 201
  • 296

1 Answers1

2

Yes, the connection will be closed unless it is a persistent connection (opened with a p: before the host). Non-persistent resources are freed automatically at the end of the script, because there are no more references to them.

From the PHP manual:

Thanks to the reference-counting system introduced with PHP 4's Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually.

Paul
  • 139,544
  • 27
  • 275
  • 264