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.
No comments:
Post a Comment