0

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 the below code?

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');  
mysql_close($link);  
?>    
Community
  • 1
  • 1
steve
  • 608
  • 1
  • 5
  • 16

2 Answers2

1

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.

Arkh
  • 8,416
  • 40
  • 45
  • From the documentation: `The link to the server will be closed as soon as the execution of the script ends`. When the client downloads the page, it has been rendered, so the mysql connection is already closed. However, if you have heavy work (in PHP) after retreiving data from the DB, it may be significant to close the connection explicitly. – Matthieu Napoli Sep 22 '11 at 13:22
  • That's if you separate your data access from your echo and other prints unless you use some buffering. Which lot of people using the mysql_ functions don't do. – Arkh Sep 22 '11 at 13:45
  • 1
    At the first echo (or equivalent), the headers and HTML will start to be sent to the client. However, the execution of the PHP script will not wait for the client downloading the page to proceed (or I am mistaken). You say `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`, it won't stay open for the client to download the page but for the PHP script to execute. – Matthieu Napoli Sep 22 '11 at 13:54
  • 1
    After some testing I stand corrected. The only important things are the executed things; php execution stops the second it gets to its last line and does not care about the data sent to the client even without explicit buffering. – Arkh Sep 22 '11 at 15:23
0

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.

Spudley
  • 166,037
  • 39
  • 233
  • 307