PHP mysqli_close() Function
Definition and Usage
The mysqli_close() function accepts a MySQL function object (previously opened) as a parameter, and closes it.
You cannot close persistent connections using this function.
Syntax
mysqli_close($con);
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
con(Mandatory) This is an object representing a connection to MySQL Server that you need to close. |
Return Values
The PHP mysqli_close() function returns a boolean value which is true on success and false on failure.
PHP Version
This function was first introduced in PHP Version 5 and works works in all the later versions.
Example
Following example demonstrates the usage of the mysqli_close() function (in procedural style) −
<?php
$host = "localhost";
$username = "root";
$passwd = "password";
$dbname = "mydb";
//Creating a connection
$con = mysqli_connect($host, $username, $passwd, $dbname);
//Closing the connection
$res = mysqli_close($con);
if($res){
print("Connection Closed");
}else{
print("There is an issue while closing the connection");
}
?>
This will produce following result −
Connection Closed
Example
In object oriented style the syntax of this function is $con->close(); Following is the example of this function in object oriented style $minus;
<?php
$host = "localhost";
$username = "root";
$passwd = "password";
$dbname = "mydb";
//Creating a connection
$con = new mysqli($host, $username, $passwd, $dbname);
//Closing the connection
$res = $con -> close();
if($res){
print("Connection Closed");
}else{
print("There is an issue while closing the connection");
}
?>
This will produce following result −
Connection Closed
Example
This is another example of the mysqli_close() function −
<?php
//Creating a connection
$con = @mysqli_connect("localhost");
$res = @mysqli_close($con);
if($res){
print("Connection closed Successfully");
}else{
print("Sorry there is an issue could close the connection ");
}
?>
This will produce following result −
Sorry there is an issue could close the connection
Example
<?php
$connection = @mysqli_connect("tutorailspoint.com", "use", "pass", "my_db");
if (mysqli_connect_errno($connection)){
echo "Failed to connect to MySQL: ".mysqli_connect_error();
}else{
mysqli_close($connection);
}
?>
This will produce following result −
Failed to connect to MySQL: No connection could be made because the target machine actively refused it.