PHP MySQL Connection Close

To close a mysql connection we can use mysql_close() function. It can takes a optional parameter as link and closes it. If no link identifier is specified then last opened connection is closed. It is not necessary to use mysql_close() function as all connections are closed at the end of the script execution. Here is the function
Note that mysql_close() will not close persistent connection created by using mysql_pconnect()

Example of MySQL connection, query execution and closing connection

The sequence is to first connect to MySQL database , then mange the database and finally close the connection.
// username and password to log onto db server 
// name of database 

function connecttodb($servername,$dbname,$dbuser,$dbpassword)
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
/// Connection is over ///////

/// Query details ///// 
$query = mysql_query("SELECT * FROM student"); 
echo "Total records in Student table= ". $number;

///// Let us close connection  //////
PHP MySQL functions Connecting to MySQL database

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    SQL Video Tutorials

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer