开发者

is the mysql_close() necessary? [duplicate]

开发者 https://www.devze.com 2023-04-06 21:54 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: close mysql connection important?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

close mysql connection important?

in the php manual, there say : Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. does it mean that we actually don't need to colse the php connect in a script?

so the code below

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');    
?>

will be more performanece than th开发者_StackOverflowe below code?

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');  
mysql_close($link);  
?>    


It is not necessary but a good thing to do is close your connection before you start sending data to the user so it does not stay open for nothing (the time for the client to download your page, which depending on his latency can take some time). heavy work on the data you got from the database.


First: No, it isn't strictly necessary in all cases. However it is good practice, and there are times when it is necessary.

I would suggest always closing any connection which you open. Even if you are confident that it isn't necessary, future changes to your code may change that, at which point you might find yourself struggling to find the bug.

To answer your second question: No, there will be zero performance benefit to be gained from not closing a connection.

In the best case scenario it will be closed automatically for you, but this still involves closing it, so it doesn't save you any time whatsoever.

In the worst case scenario, not closing a database connection can result in your application running out of available connections, which will obviously hurt your site quite lot.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号