What Is PDO and how to connect using PDO In PHP?

The term PDO resembles for PHP Data Object .

In short , PDO extensions defines a set of php extensions which provides the interface for accessing the database in php.

It is the light weight process .

It provides the core PDO class and databases for specific drives.

Each drivers that implements the PDO interface act as the database-specific fearures as regular extension functions.

PDO provides us the vendor-neutral, lightweight, data-access abstraction layer , so for this reasons , it doesn’t matter which database we are using , you can use the same functions and to issue the queries and fetch the data .

PDO does not focus on database abstraction rather on data access abstraction.

The basic requirement for PDO is the core of php 5+, i.e the new object oriented features , So for this reasons it will not execute with earlier versions of PHP .

The PDO consists of components

The core for interface and Drivers to access the certain driver.

Let Set the connection using PDO : 

<?php
     $hostname = “localhost”; 
     $username = “username”;
     $password = “password”;

try {
    $conn = new PDO(“mysql:host=$hostname;dbname=myDB”, $username, $password);
    // seting the PDO error to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo “Connection established successfully”; 
    }
catch(PDOException $e)
    {
    echo “Failed to Connect: ” . $e->getMessage();
    }

?>




1 thought on “What Is PDO and how to connect using PDO In PHP?

Leave a Reply

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