Learning Resources
 

Database connection connect and close


Create a Database Connection - To create a connection to the database in PHP, is done by mysql_connect() function.

Syntax
mysql_connect(servername,username,password);

Parameter     Description
servername     Optional. Specifies the server to connect to. Default value is "localhost:3306"
username     Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process
password     Optional. Specifies the password to log in with. Default is ""

An example -
$con = mysql_connect("localhost","peter","abc123");


Closing a Database Connection - The connection will be closed automatically when the script ends. To close the connection before, PHP uses the mysql_close() function:

Syntax
mysql_close(connection handle);

An example -
mysql_close($con);