Users Pricing

blog

Home Blogs How to connect with database in PHP – MindStick

How to connect with database in PHP

Royce Roy 2624 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


Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md