PHP | mysqli_close() Function

MySQLi Procedural procedure:
To close the connection in mysql database we use php function mysqli_close() which disconnect from database. It require a parameter which is a connection returned by the mysql_connect function.

Syntax:

mysqli_close(conn);

If the parameter is not specified in mysqli_close() function, then the last opened database is closed. This function returns true if it closes the connection successfully otherwise it returns false.

Below program illustrate the mysqli_close() function

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$servername = "localhost";
$username = "username";
$password = "password";
  
// Creating connection
$conn = mysqli_connect($servername, $username, $password);
  
// Checking connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
  
// Creating a database named newDB
$sql = "CREATE DATABASE newDB";
if (mysqli_query($conn, $sql)) {
    echo "Database created successfully with the name newDB";
} else {
    echo "Error creating database: " . mysqli_error($conn);
}
  
// closing connection
mysqli_close($conn);
  
?>

chevron_right


MySQLi Object-oriented procedure::



To close the connection in mysql database we use php function conn->close() which disconnect from database.

Syntax:

conn->close();

Program: To illustrate the closing of connection in object-oriented procedure.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "newDB";
  
// checking connection
$conn = new mysqli($servername, $username, $password, $dbname);
  
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//Close the connection
$conn->close();
?>

chevron_right


Using PDO procedure:
To close the connection in MySQL database in PDO procedure we set the connection name to null which disconnect from the database.

Syntax:

conn=null;

Program: to illustrate the closing of connection in PDO procedure.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$servername = "localhost";
$username = "username";
$password = "password";
  
try {
    $conn = new PDO("mysql:host=$servername;dbname=newDB"
                     $username, $password);
  
    // setting the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
    $sql = "CREATE DATABASE newDB";
  
    // using exec() because no results are returned
    $conn->exec($sql);
  
    echo "Database created successfully with the name newDB";
    }
catch(PDOException $e)
    {
    echo $sql . "
" . $e->getMessage();
    }
$conn = null;
?>

chevron_right


References: http://php.net/manual/en/mysqli.close.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.