PHP 5 Mysql Database Connection Methods

Introduction :

On the previous lesson i explained about PHP basics and its advantages and dis-advantages . On this lesson we are going to learn about a function called "mysql_connect" This function will allow us to connect our PHP program with the back-end Mysql Database . 

Example for Mysql_Connect() Function :

$conn=mysql_connect("hostname", "username ", "password"); 
The above code will help you to connect your PHP program with Mysql Database .

Hostname :

Hostname was indicating the server . If you are running both Mysql Database and Php program in the same server . Then the host name will be "localhost" .

Username :

Username indicating the "Username" Of your Mysql Database . If the "Username" Was incorrect , The program can't access to Database .

Password :

Every Database have a password which set by the administrator of the password . If the password was incorrect , PHP Program can't access to your database .

Full Functioned Example for Mysql_Connect() Function :

       $user = "root";
       $pwd = " ";
       $host = "localhost";
       $conn = @mysql_connect($host, $user, $pwd);
       echo "Successfully connected";
       echo"connection fail";
The above code will help you to connect your PHP Program with Mysql Database . Just create a new Php page on your server and try the code . Don't forget to change the variable with your database details  .

If the connection establish with you database by the code . Then "Successfully Connected" Message will show on the screen . If it was not , Then the "Connection Fail" Message will show .

If the connection was failed , Try to check the variable that you assigned for $user , $pwd , $host  .

PHP 5 Mysqli_Connect() Function :

$con = mysqli_connect("localhost","my_user","my_password","my_db");
In PHP 5 the Mysql_Connect() method was changed to Mysqli_Connect() method . All other things was same as before .

Suggested Article for PHP Developers .

I hope you well enjoyed this article . If you have any doubts or suggestions about this article . Please let us know by using the comment box given below . Thanks