English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_close() Function Usage and Example

PHP MySQLi Reference Manual

mysqli_close() functionClose the previously opened database connection

Definition and Usage

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.

Syntax

mysqli_close($con);

Parameter

Serial NumberParameters and Description
1

con(Required)

This is an object representing the connection to the MySQL Server that you need to close.

Return Value

The PHP mysqli_close() function returns a boolean value, which is true on successtrue, on failurefalse.

PHP version

This function was originally introduced in PHP version5introduced and can be used in all higher versions.

Online Example

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

Online Example

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

Online Example

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

Online Example

<?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.

PHP MySQLi Reference Manual