When a PHP script completes execution, any open mysqli_connect()
instances will be closed automatically to help beginners free up
resources in case they happen to forget to close a connection. If we
want to make sure that all resources are freed up after we query the
database, we use the mysqli_close()
function. This will assure that the connection is closed at the exact
line in the script where you write it in.
Here is an example of using mysqli_close()
to close a mysql connection that we have open:
<?php
// Include the connection script to connect to the database in any script
require_once "connect_to_mysql.php";
// Now you can use the variable $myConnection from our include script
// Execute all queries needed then close your connection
// Run the close connection function
mysqli_close($myConnection);
// If you attempt a query here in your script it will fail
?>