English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
mysqli_close() functionClose the previously opened database connection
mysqli_close();The function accepts a MySQL function object (previously opened) as a parameter and closes it.
You cannot use this function to close Persistent connection.
mysqli_close($con);
Serial Number | Parameters and Description |
---|---|
1 | con(Required) This is an object representing the connection to the MySQL Server that you need to close. |
The PHP mysqli_close() function returns a boolean value, which is true on successtrue, on failurefalse.
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
The following examples demonstratemysqli_close();Usage of the function (procedural style)-
<?php $host = "localhost"; $username = "root"; $passwd = "password"; $dbname = "mydb"; //Establish Connection $con = mysqli_connect($host, $username, $passwd, $dbname); //Close Connection $res = mysqli_close($con); if($res){ print("Connection closed"); }else{ print("Sorry, there may be an issue that could close the connection"); } ?>
Output Result
Connection closed
In the object-oriented style, the syntax of this function is$con-> close();.Here is an example of this function in an object-oriented style;
<?php $host = "localhost"; $username = "root"; $passwd = "password"; $dbname = "mydb"; //Establish Connection $con = new mysqli($host, $username, $passwd, $dbname); //Close Connection $res = $con -> close(); if($res){ print("Connection closed"); }else{ print("Sorry, there may be an issue that could close the connection"); } ?>
Output Result
Connection closed
This ismysqli_close();Another example of the function-
<?php //Establish Connection $con = @mysqli_connect("localhost"); $res = @mysqli_close($con); if($res){ print("Connection closed"); }else{ print("Sorry, there may be an issue that could close the connection"); } ?>
Output Result
Sorry, there may be an issue that could close the connection
<?php $connection = @mysqli_connect("w3"codebox.com", "use", "pass", "my_db"); if (mysqli_connect_errno($connection)){ echo "Connection to MySQL failed: " . mysqli_connect_error(); }else{ mysqli_close($connection); } ?>
Output Result
Unable to connect to MySQL: Connection cannot be established because the target computer actively rejected the connection.