PHP & MySQL: Closing Connection to MySQL Database

In the page, PHP & MySQL: Creating Connection to MySQL Database, I discussed how to create a connection. mysql_close() function is used to close the connection. Actually, the connection is closed automatically when the scripts end. But it is a good practice to close the connection. This page will discuss about the connection closing process. Keep reading...

How to close a connection?

The amazing thing is that MySQL doesn't need to close the database connection. The process is fully automatic. When the script is ended, it automatically close the connection.

But sometimes, it may remain open the connection. So, it's a good idea to close the connection manually. I preferred this.

To close the connection of MySQL database one function is used. It's mysql_close() function. Just use this function to terminate or close MySQL database connection.

Example to close the connection

Use mysql_close() function to close the connection. One example is provided below for easy understanding:

<?php

$myConnection = mysql_connect("localhost","user","abc123");

if (!$myConnection)
  {
    die('Not possible to connect...' . mysql_error());
  }

/* all other codes */

mysql_close($myConnection);

?>

From the above code, you see that mysql_close($myConnection) is used to close the connection. Here, $myConnection is a variable which is defined before to make a connection. By using mysql_close() function, the connection is terminated.

Note: To learn more about the code, please read PHP & MySQL: Creating Connection to MySQL Database.

In the next MySQL related tutorial, I will try to explain the process of creating a database by using PHP and MySQL.

© Written by rancidTaste

If you are enjoyed this post, please consider to give a thumbs up and leave a comment. You may Subscribe to rancidTaste's RSS feed to get new pages which will be delivered to your feed reader. You can also read more hubs by rancidTaste.

Want to express your knowledge and ideas, click here to join Hubpages.

It's simple, just like 1-2-3

More by this Author


Comments 1 comment

Kelly Jones 4 years ago

Perfect post. Here’s a tool that lets your build your online database without programming. There is no need to hand code PHP. Cut your development time by 90% http://www.caspio.com/

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working