codescracker


php

PHP and MySQLi Close Database Connection



« Previous Tutorial Next Tutorial »


To close database connection or to close any connection to the MySQL database using PHP and MySQLi, use mysqli_close() function.

What to do Before Closing Database Connection

Before closing the database connection by example code given below in this tutorial using PHP and MySQLi, make sure that you have already learned and created a database and a table namely codescrackerTwo and codescrackerTable (with or contains 4 columns) and inserted some data inside it.

PHP MySQLi Close Database Connection Syntax

Here is the general form used to close database connection using PHP.

mysqli_close($connection_link_identifier);

Here $connection_link_identifier is the identifier that is used to open the database connection. Let's take an example illustrating it. Here we are using the variable named $conn to open and close the database connection.

PHP MySQLi Close Database Connection Example

Here is an example shows how to close a database connection using PHP and MySQLi.

<?php 
	// below code will open database connection to codescrackerTwo
	$conn = mysqli_connect('localhost', 'root', '', 'codescrackerTwo');
	if(mysqli_connect_error())
	{
		echo "<p>Error occurred in database connection.</p>";
		exit();
	}
?>
<html>
<head>
	<title>PHP and MySQLi Close Database Connection Example</title>
</head>
<body>

<?php 
	$qry = "select * from codescrackerTable";
	// below code will query the above statement
	// on table named codescrackerTable present in 
	// database named codescrackerTwo
	$res = $conn->query($qry);
	if($res)
	{
		// below codes will fetch and display all the records 
		// present inside the table named codescrackerTable
		while($row = mysqli_fetch_row($res))
		{
			// as the third row contains user comment value
			// and second row contains the user id value
			echo "<i>$row[3]</i> by <b>$row[2]</b><br/>";
		}
	}
	else 
	{
		echo "error occurred in executing the above query.";
		exit();
	}
	// below code will close the database connection
	mysqli_close($conn);
	echo "<hr/>";
	// now again perform the above query and try to display the 
	// record of the same table and database
	// here this time you will find some error and the following 
	// query will not be performed as you have already closed 
	// the connection to the database
	$qry = "select * from codescrackerTable";
	$res = $conn->query($qry);
	if($res)
	{
		while($row = mysqli_fetch_row($res))
		{
			echo "<i>$row[3]</i> by <b>$row[2]</b><br/>";
		}
	}
	else 
	{
		echo "<br/>no query can be executed after closing the database connection.";
		exit();
	}
?>

</body>
</html>

Here is the sample output produced by the above example code of closing database connection using PHP and MySQLi.

php mysqli close database connection

As you can see from the above example and its output, when you close the database connection, then after that, you can't perform any action to that database. And if you do after closing the database connection, then you will find some error in executing the query as shown in the above screenshot.


« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test