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

How to connect with database in PHP


PHP PHP  MYSQL 
Ratings:
0 Comment(s)
 154  View(s)
Rate this:

How to connect with database in PHP ?


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:



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

?>


Don't want to miss updates? Please click the below button!

Follow MindStick