简体   繁体   中英

Closing a MySQL connection in PHP

I was just wondering, is it good to close a MySQL connection in PHP at the end of your page? What are the advantages? Does it matter to close it?

Sincerely, Joey

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution, but also remember 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

PHP will release any resources at the end of a script anyway.

It is good to release file resources and locks on files for example because your script may run form 1s but you may need the file only for a few ms at the very beginning of your script.

Using the same reasoning if you use SQL for a few ms at the beginning then only do processing on the data extracted - by closing your connection you're allowing another connection to take your place (there are MySQL limits to how many simultaneous connections can be made).

If on the other hand you release it at the end of the script because you use the DB (intermittently) during the entire execution of your script then you're just doing what PHP will do anyway at the end of the script.

yes ,it is good practise to close the connection after use it removes the burden from database .perfomance will improve . one should always release the resources after use so that they can be be used by other users.

If you mean using persistent connections: in general (99.99% of the time) it is NOT (!) very wise to use persistent connections. Certainly not when using transactions. The performance penalty for opening and closing connections every request is not that big.

PHP persistent connections are bad because...

  • they cause transactions, table locks, temporary tables, session variables and most other useful features in MySQL to be very dangerous, potentially causing server-wide deadlocks and database errors during page generation.
  • they occupy hundreds of MySQL sockets and threads, increasing the risk of hitting a limit somewhere (open files, mysql settings, kernel limits?).
  • When something break because of persistent connections, it's certain to be extremely difficult to diagnose, since it will only show up after a certain thread has served certain requests in a certain order.
  • with a pool of web servers, one slow web server can back up and consume so many connections it can't use that the other servers can't create connections to complete their requests.

See for instance this article.

http://meta.wikimedia.org/wiki/Why_persistent_connections_are_bad

In general it is also good programming practice to explicitly release all resources you acquire as soon as possible since they can then be used by competing scripts of routines. Using constructors and destructors ('smart resources') is a good way to do this. In PHP it is possible your script ends with a fatal error in which case you get no chance to release them yourself.

$conn = new mysqli("localhost","Username","Password","Db_name");

$sql = "SELECT * FROM `your_table_name`";

$connStatus = $conn->query($sql);

$numberOfRows = mysqli_num_rows($connStatus);

$numberOfRows; 

//this echo out the total number of rows returned from the query

$conn->close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM