views:

126

answers:

5

Possible Duplicate:
using mysql_close()

Are mysql_close and pg_close required ?

In some script there aren't... why ?

What happen if I don't use its ?

+2  A: 

duplicate

http://stackoverflow.com/questions/2065282/using-mysql-close

Haim Evgi
i'd like to give +0.5 because there's a part about pg_close which is non duplicate. ahh, better round it up. :)
Unreason
pg_close its the same like mysql_close :)
Haim Evgi
+1  A: 

You do not release resources explicitly, but rely on timeouts on the server side and internal housekeeping on the application side.

Unreason
+1  A: 

PHP will close any open connection at the end of the execution. So, nothing happens if you do not put it.

See http://www.php.net/manual/en/function.mysql-close.php

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

nico
This depends on PHP's garbage collector detecting a freeable resource and tidying up. You can't guarantee when that cleanup will happen and I've seen systems where it takes altogether far too long. For balance, I've also seen systems where it happens nigh-on the instant the execution finishes too... But why take the risk?
Oli
@Oli It doesn't. Unless you're using persistent connections, the connection will go away on request shutdown. It can even go earlier, for instance, if all the variables that refer to the connection are unset or go out of scope. The purpose of the garbage collector introduced in 5.3 is to cleanup resources that have circular references. If you've seen different behavior, submit a bug report.
Artefacto
+4  A: 

Why don't you read it in the PHP reference manual? All information is there...

But in a nutshell : no they are not necessary, but imho it's better to close the connections yourself to free resources asap when you no longer need them.

wimvds
+1 for not needed, but a good idea if a script runs longer / freeing resources asap.
Wrikken
+1  A: 

If you don't call it, the socket connection to the database remains open for ~30 seconds in a wait state.

If you get lots and lots of people and you don't somehow manage to reuse these zombie connections, your database might explode with a too many users error.

So in answer to your question: syntactically not required but it's very poor practice not to include them.

Oli
See my comment in nico's answer.
Artefacto
Are you sure the database will explode? I would expect application not to be able to make new connections but the database to keep running and become available after those ~30 seconds pass for all open connections.
Unreason
@Unreason: That's much more likely, yes. But the end result is the same under heavy load: website downtime.
Oli