PHP Database Connection with MySQL Database

PHP Database Connection with MySQL Database

Create PHP Database Connection with MySQL Database

Hi, in this tutorial you will learn how to make a database connection with PHP.

In all the examples below, we will create a connection with the MySQL database.

1 – MySQLi Procedural way to connect database

<?php
$connection = mysqli_connect("localhost","db_user","db_password","db_name");
// CHECK DATABASE CONNECTION
if(mysqli_connect_errno()){
    echo "Connection Failed".mysqli_connect_error();
}
else{
    echo "The database is successfully connected";
}
?>

2 – MySQLi OOP (Object Oriented) way to connect database

<?php
$db_conn = new mysqli("localhost","db_user","db_password","db_name");
// CHECK DATABASE CONNECTION
if($db_conn->error === false){
    echo "Connection Failed - ".$db_conn->connect_error;
}
else{
    echo "The database is successfully connected";
}
?>

3 – Connect database with PHP PDO

<?php
$db_host = 'localhost';
$db_user = 'db_user';
$db_password = 'db_password';
$db_name = 'db_name';

// DSN(Database Source Name)
$dsn = 'mysql:host='.$db_host.';dbname='.$db_name.';charset=utf8';

try{
   $db_conn = new PDO($dsn, $db_user, $db_password);
   // SET THE PDO ERROR MODE TO EXCEPTION
   $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e){
   echo $e->getMessage();
   exit;
}
?>

How to close Database connections –

The database connection will be automatically closed when the script ends. But if you want to close it earlier then use the following:

Use mysqli_close() function to close the database connection with the MySQLi Procedural way.

<?php
$connection = mysqli_connect("localhost","db_user","db_password","db_name");
// CHECK DATABASE CONNECTION
if(mysqli_connect_errno()){
    echo "Connection Failed".mysqli_connect_error();
}
else{
    echo "The database is successfully connected";
}
// CLOSE THE CONNECTION
mysqli_close($connection);
?>

Close the connection with the MySQLi OOP way.

<?php
$db_conn = new mysqli("localhost","db_user","db_password","db_name");
// CHECK DATABASE CONNECTION
if($db_conn->error === false){
    echo "Connection Failed - ".$db_conn->connect_error;
}
else{
    echo "The database is successfully connected";
}
// CLOSE THE CONNECTION
$db_conn->close();
?>

PHP PDO close the databse connection –

<?php
$db_host = 'localhost';
$db_user = 'db_user';
$db_password = 'db_password';
$db_name = 'db_name';

// DSN(Database Source Name)
$dsn = 'mysql:host='.$db_host.';dbname='.$db_name.';charset=utf8';

try{
   $db_conn = new PDO($dsn, $db_user, $db_password);
   // SET THE PDO ERROR MODE TO EXCEPTION
   $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e){
   echo $e->getMessage();
   exit;
}
// CLOSE THE CONNECTION
//unset the $db_conn value
unset($db_conn);
//OR you can set value null
// $db_conn = null;
?>

Read also –

Posts Navigation


Leave a Reply

Your email address will not be published. Required fields are marked *