about closing database connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bugthefixer
Forum Contributor
Posts: 118
Joined: Mon Mar 22, 2004 2:35 am

about closing database connection

Post by bugthefixer »

do we explicitly need to close connection

mysql_close($link)

or connection is automatically closed after execution of each page.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

AS far as I am aware connections should get closed automatically in MySQL, I know this is the case with postgres. However it is good design practice to always ensure it is closed. It is not all that hard to do after all.

Normally you have three layers
<database load and save>
<process>
<output>.

Rather than wait for the output I tend to close the connection after either the database layer (if process does not touch the database which I try to avoid) or after the process layer but before the output.

It is the same with freeResult(). You shouldn't have to do it but it is good practice to.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

According to php's manual :

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources.
Post Reply