MySql Connect : The mysql_connect() function.

MySql is the database mostly paired with PHP to provide robust functionality of Interactive webpages.To establish any communication with database in MySql, it is necessary to connect with the database.

The PHP mysql_connect() function is used to open a connection with MySql database.The PHP mysql_connect() function takes three parameters i.e server name,database username and database user password.If the PHP mysql_connect() function fails to connect with the database it returns false.The reason of why the connection was unsuccessful, can be retrieve by the PHP mysql_error() function.The example below will be useful.

$hostname = "localhost";
$username = "zohaib";
$password = "12345";
$connection = mysql_connect($hostname,$username,$password);
if (!$connection)
 echo "error :".mysql_error();
echo "Connection was successfull";

In this example a connection to the MySql database is attempted if the connection is ok the page will display "Connection was successfull" otherwise it displays the error.The returned value by the function is stored in a variable $connection this variable is tested in the if statement.If $connection variable is false i.e connection fails, the negation operator ! invert it to true and the error is echoed.



Post a Comment