Tuesday, April 29, 2008

MySQL Connection using PHP Script:

MySQL Connection using PHP Script:

PHP provides mysql_connect() function to open a database connection. This function takes five parameters and returns a MySQL link identifier on success, or FALSE on failure.

Syntax:

connection mysql_connect(server,user,passwd,new_link,client_flag);

You can disconnect from MySQL database anytime using another PHP function mysql_close(). This function takes a single parameter which is a connection returned by mysql_connect() function.

Syntax:

bool mysql_close ( resource $link_identifier );


Example:
<html> 
<head>
<title>Connecting MySQL Server</title>
</head>
<body>
<?php $dbhost = 'localhost:3036';
$dbuser = 'guest';
$dbpass = 'guest123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

mysql_close($conn);?>

</body></html>