Users Pricing

blog

home / developersection / blogs / how to connect with database in php

How to connect with database in PHP

Royce Roy 2434 04 Jun 2016 Updated 15 Mar 2018

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

Royce Roy

Other


1 Comments