close
In this article, we will focus on the mysqli_close() function in PHP, which is used to close a MySQLi connection. We will provide you with an overview of the
PHP mysqli_close() Function
In this article, we will focus on the mysqli_close() function in PHP, which is used to close a MySQLi connection. We will provide you with an overview of the function, how it works, and examples of its use.
Introduction to the mysqli_close() function
The mysqli_close() function is a built-in procedural function in PHP that is used to close a MySQLi connection. In the object-oriented MySQLi API, the equivalent operation is performed by calling the close() method on a mysqli object. Both approaches are useful when you need to release resources or when you are done with a specific connection.
How to use the mysqli_close() function
Using the MySQLi connection close operation is very simple. You just need to call the appropriate function or method on a valid connection object. Here is an example using the object-oriented API:
How to use the mysqli_close() function
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
exit();
}
// execute queries using the connection
if (!$mysqli->close()) {
echo "Connection close failed.";
}
?>In this example, we create a new MySQLi object and connect to a MySQL database with a username and password. We can then execute queries using the connection. Finally, we close the connection by calling the close() method of the MySQLi object. Note that the method returns true on success and false on failure. If not explicitly closed, PHP automatically cleans up the connection when the script ends.
For the procedural API, the equivalent syntax is mysqli_close($link);, where $link is the connection resource returned by mysqli_connect(). Here is a procedural example:
Procedural usage of mysqli_close()
<?php
$link = mysqli_connect("localhost", "username", "password", "database");
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
// execute queries using the connection
if (!mysqli_close($link)) {
echo "Connection close failed.";
}
?>Advanced usage
The MySQLi close operation can also be used in more advanced scenarios. For example, you can use it to release resources used by a specific connection or to close multiple connections at once. Here is an example:
Advanced usage of PHP mysqli_close()
<?php
$mysqli1 = new mysqli("localhost", "user1", "pass1", "database1");
$mysqli2 = new mysqli("localhost", "user2", "pass2", "database2");
// execute queries using the connections
$mysqli1->close();
$mysqli2->close();
?>In this example, we create two new MySQLi objects and connect to two different MySQL databases with different usernames and passwords. We can then execute queries using both connections. Finally, we close both connections by calling the close() method of each MySQLi object.
Note on persistent connections: mysqli_close() cannot close persistent connections. Persistent connections remain open after the script ends and are managed by the connection pool.
Conclusion
In conclusion, the mysqli_close() function (and its OOP equivalent close() method) is a powerful tool for closing MySQLi connections in PHP. By understanding how to use the function and its advanced usage scenarios, you can take advantage of this feature to release resources and manage database connections efficiently in your PHP scripts.
Practice
What is the purpose of the close() function in PHP?