Before we can access the data in MYSQL database,we have to first connect the server.PHP provides mysqli_connect() function to connect with database. This function has five parameter:


How to connect with database in PHP

Example: MYSQLi  object-oriented
<?php
 
// Create connection
 
$connect=new mysqli(“localhost”,”username”,”password”);
 
//check connection
 
If($connect->connect_error)
{
     die(“Connection failed :”  . $connect->connect_error);
}
else
{
     echo(“connection established”);
}
?>

Note: In above example $connect_error is used untill PHP  5.2.9 and 5.3.0. If you are using PHP version other than 5.2.9 and 5.3.0 use following code:

<?php
 
// Create connection
$connect = mysql_connect(“localhost””,”username”,”password”);
 
//Check connection
 
If($connect->connect_error)
{
     die(“Connection failed :”  . mysqli_connect_error());
}
echo(“Connection established”);
?>
  Modified On Mar-15-2018 04:36:50 AM

Leave Comment