Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL Database Connection

Tags:

php

mysql

Is it necessary to explicitly close a DB connection after executing a query (and other DB operation)?

like image 390
Sanny Avatar asked Aug 01 '26 21:08

Sanny


2 Answers

No, php does that automatically. You could call it "good programming practice" to clean up (aka close the connection) though.

like image 161
Robus Avatar answered Aug 03 '26 11:08

Robus


Generally, only close it after you have finished the database for that entire page. Also, depending on the API you are using to handle the database, most ones you should be using now (such as PDO) are implemented via Objects, so the destructor will automatically handle closing it at the end if needed.

like image 31
Dan McGrath Avatar answered Aug 03 '26 09:08

Dan McGrath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!