Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_close() Function.
The PHP mysqli_close() function accepts a MYSQL function object (formerly opened) as a parameter, and closes it.
You cannot close persistent connections using this function.
The PHP mysqli_close() function accepts a MYSQL function object (formerly opened) as a parameter, and closes it.
You cannot close persistent connections using this function.
Syntax
Following below is the syntax to use this function -
mysqli_close($con);
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | con(Mandatory) This is an object representing a connection to MySQL Server that you need to close. |
Return Value
This built-in PHP function returns TRUE on success and FALSE on failure.
PHP Version
This built-in function was first introduced in PHP version 5 and it works in all of the later versions.
Example1
The following example demonstrates the usage of the PHP mysqli_close() function (in a 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"); } ?>
Output
When the above code is executed, it will produce the following result -
Connection Closed
Example2
In an object oriented style the syntax of this PHP function is $con->close(), Following is the usage of this PHP function in an 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"); } ?>
Output
When the above code is executed, it will produce the following result -
Connection Closed
Example3
This is another example of the built-in PHP 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 "); } ?>
Output
When the above code is executed, it will produce the following result -
Sorry there is an issue could close the connection
Example4
Try the following example below -
<?php $connection = @mysqli_connect("webdesigntutorialz.com", "use", "pass", "my_db"); if (mysqli_connect_errno($connection)){ echo "Failed to connect to MySQL: ".mysqli_connect_error(); }else{ mysqli_close($connection); } ?>
Output
When the above code is executed, it will produce the following result -
Failed to connect to MySQL: No connection could be made because the target machine actively refused it.
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP mysqli_commit() Function.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.