Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 4 April 2016

Establish connection with MySql Database through PHP.

Establish connection with MySql Database through PHP.

1) Select Database


  MySqli_connect("HostName","Username","Password","DatabaseName") // Recommended to use

                                                                 OR

  MySqli_connect("HostName","Username","Password","DatabaseName","Port","Socket")

2) Store above Connection line OR Object in a PHP Variable.


 $con=  MySqli_connect("HostName","UserName","MySql_Password","DatabaseName");

              // for Localhost:- HostName= Localhost, {also some hosting site like:- Microhost is                              //also use HostName as Localhost.}
              //                          Username= root, 
              //                          Password = Null,
              //                          Port=3306
              //                          Socket=Specifies the socket or named pipe to be used

3) Check whether the connection is established or Not !, For this use 'IF-ELSE' condition.


 if(!$con) // It is same as $con == 0 or $con != 1; or return FASLE. So query will stop here.
{
die("Not Connected");  // die Language Construct( Not a Function ) will  exits the current script.
                               //{ We always recommended that always use either ECHO or PRINTF and                                          // show/Handle an error Like:- Database is not connected at this time ..                                                 //Please try later..... So the whole script will running continue instead of                                                 //Breaks your whole  script. }
}
    echo "Database is connected with your PHP Web-Page"   ;
                           //Now there is not need to define any ELSE condition here because if 'IF'                                              // condition is not executed then content after closing of 'IF'  will be execute.

4) If point 3) is not execute then it mean your connection is established otherwise check your Connection Line or Parameter you write in point 1).

5) Now your connection from MySql Database is established, and in next Tutorial we will discuss about Retrieve information from MySql into the Web-Page.

No comments:

Post a Comment