8

is it necessary to use mysql_close() at the end of a query in PHP?

phpFreak
  • 81
  • 1
  • 2

4 Answers4

18

In the manual :

mysql_close() closes the non-persistent connection to the MySQL server that's associated with the specified link identifier.

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

More reading about that here

marcgg
  • 65,020
  • 52
  • 178
  • 231
  • 3
    Terminating resources as soon as they're no longer required is always a good practice. If your script is long running holding on to resource you no longer need just brings you closer to resource exhaustion. – preinheimer Jan 14 '10 at 15:54
5

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.

read more at :

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

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • 1
    Performance can be improved by closing as soon as you are done so the handle is available for other processes to use. – Adeel Aug 11 '10 at 05:39
3

As answered by others and the manual, it's not necessary. But if you wonder the use; you usually only want to do this when there is more to come in the PHP script and you want to ensure that another connection/transaction is to be used then.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

No. When the PHP requests ends all resources will be freed, including MySQL connection resources.

c0deaddict
  • 151
  • 1
  • 2