Our database Configuration –
Database name – microcodes
User-name – root
Password – NULL
Host – localhost
  • For getting ip-address of your local machine, open CMD (in windows) type command: ipconfig

Then, copy the address of IPV4 address. Your case this address may looks different. But you can use ‘localhost’, there should not any problem, until you to connection with remote database.

  • For doing a connection with database we need 4 arguments –
  • host-name  2.username  3.database’s password  4.database name

mysqli_connect() -> this method helps to establish connection with MySQL database only.

First, open XAMPP server and start service ‘apache2’,’mysql’.

Write a PHP code, to create connection between a PHP file with MySQL database.

File – code1.php

<?php
$conn = mysqli_connect(‘localhost’,’root’,”,’microcodes’);
if($conn){
    echo “connection established!”;
}
else{
    echo “connection error!”;
}
?>
output

  • Let’s another way of writing this code…

File – code2.php

<?php
// my localhost ip-adress: 192.168.56.1
$host=’localhost’;
$user=”root”;
$passd=””;
$db=”microcodes”;
 
$conn = mysqli_connect($host,$user,$passd,$db);
if($conn){
    echo “connection established!”;
    echo “<br><br> <font size=’20px’ color=’green’>
    You can now do any further operation over this connection, like CRUD.
    </font>”;
}
else{
    echo “connection error!”;
}
?>
output

But what about different databases like Oracle SQL/Ms-SQL/PostgreSQL etc.

Suppose, you build entire backend arquitechture using MySQL. But for some reason, you want to switch whole database.so, here from the first time

You should use PDO.

  • PDO?

Ans – PDO is more secure than the first two options and it is also faster in comparison with MySQLi procedural and MySQLi object-oriented.

PDO is a database access layer that provides a fast and consistent interface for accessing and managing databases in PHP applications. Every DBMS has a specific PDO driver that must be installed when you are using PDO in PHP applications.

  • What is the difference between MySQLi and PDO?

As stated earlier, both PDO and MySQLi are extremely similar, but there’s slight differences in syntax. MySQLi follows the old-school PHP snake_case convention, while PDO uses camelCase. Additionally, MySQLi’s methods are used as object properties, while PDO uses the traditional syntax for functions.08-Jun-2018

We will connect MySQL DB with our PHP script. Let’s see the code…

  • Let’s write a PHP code, to see the usage of PDO to establish a connection between PHP and MySQL DB.

File – code3.php

<?php
$dsn=”mysql:host=localhost;dbname=microcodes”;
$user=”root”;
$passd=””;
 
try{
    $conn = new PDO($dsn,$user,$passd);
   echo “database connected”;
   echo “<br> <font size=’20px’ color=’dark-violet’>
   database ‘MySQL’ connected via PDO.
   </font>”;
}
catch(PDOException $e){
    echo “connection failed!”;
    echo $e;
}
 
?>
output

  • Write a PHP code, to connect a PHP file with PostgreSQL DB & Oracle DB and other databases as well.

File – code4.php

<?php
// connect with PostgreSQL DB via PDO
$dsn=”pgsql:host=localhost;dbname=microcodes”;
$user=”root”;
$passd=””;
 
try{
    $conn = new PDO($dsn,$user,$passd);
   echo “database connected”;
   echo “<br> <font size=’20px’ color=’dark-violet’>
   database ‘MySQL’ connected via PDO.
   </font>”;
}
catch(PDOException $e){
    echo “connection failed!”;
    echo $e;
}
?>
 
<?php
// connect with oracle DB via PDO
// syntax – “driver_name=//host_name/database_name”
$dsn=”oci:dbname=//localhost:1521/microcodes”;
$user=”root”;
$passd=””;
 
try{
    $conn = new PDO($dsn,$user,$passd);
   echo “database connected”;
   echo “<br> <font size=’20px’ color=’dark-violet’>
   database ‘MySQL’ connected via PDO.
   </font>”;
}
catch(PDOException $e){
    echo “connection failed!”;
    echo $e;
}
?>
  • Connection close

One of the important thing, when your work done, you should close your connection.

When your database should use at any case, then it’s better to close the connection.

We consider, MySQL DB at this case, but same thing will follows for other databases as well.

Syntax –

mysqli_close(connection_name);

  • Let’s see, how to close connection using mysqli_close() method.

File – code5.php

<?php
$conn = mysqli_connect(‘localhost’,’root’,”,’microcodes’);
if($conn){
    echo “connection established!”;
}
else{
    echo “connection error!”;
}
// closing the mysql connection
mysqli_close($conn);
 
?>
  • Let’s see, how to close connection using object-oriented style

File – code6.php

<?php
$mysqli = new mysqli(“localhost”,”root”,””,”microcodes”);
 
if ($mysqli -> connect_errno) {
  echo “Failed to connect to MySQL: ” . $mysqli -> connect_error;
  exit();
}
 
$mysqli -> close();
?>