blog

Home / DeveloperSection / Blogs / How to connect with database in PHP

How to connect with database in PHP

Royce Roy 2057 04-Jun-2016

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”);
?>

php php  mysql 
Updated 15-Mar-2018

Leave Comment

Comments

Liked By