Open In App

What is PDO in PHP ?

In this article, we will discuss PDO in PHP in detail.

There are three main options for 






<?php
    try {
        $dbhost = 'localhost';
        $dbname='gfg';
        $dbuser = 'root';
        $dbpass = '';
        $connect = new PDO(
"mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    }
   catch (PDOException $e) {
        echo "Error : " . $e->getMessage() . "<br/>";
        die();
    }
    $count = $connect->exec("DELETE FROM employee WHERE emp_id = 123 ");
    echo("Number of rows to be deleted from the employee table is: ". $count);
?>

ting to a MySQL database server.

MySQLi procedural and MySQLi object-oriented only support MySQL database but PDO is an advanced method along with MySQL which supports Postgres, SQLite, Oracle, and MS SQL Server.



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.

It simplifies the database operations including:

When we want to connect PHP and MySQL then we need to follow 3 steps:

Steps for connection using PDO.

Connection:

In this step, we connect with the database

$conn = new PDO($db_name,$username,$password);

When we want the connection between PHP and MySQL using PDO then we need to create an object of the PDO class.

$db_name = "mysql:host=localhost;dbname:gfg";

Note: $db_name contains the following information.

Run SQL query: To run SQL query we can use two methods:

$sql = $conn->query("select * from students");
 $sql = $conn->prepare("select * from students");

Note: The prepare() method is more secure than the query() method.

Close the connection:

For closing the database connection using PDO.

$conn = null;

Parameters: It contains the following parameters.

Return Value:

On success, it will return the PDO object. And on failure, it will return the PDOException object.

Handling error during connection:

The PDOException object will be thrown in case of any error. We can catch the exception to handle the error. 




<?php
    try {
          $dsn = "mysql:host=localhost;dbname=gfg";
        $user = "root";
        $passwd = "";
        $pdo = new PDO($dsn, $user, $passwd);
    }
    catch (PDOException $e) {
         echo "Error!: " . $e->getMessage() . "<br/>";
         die();
     }
?>

Output:

Error : SQLSTATE[28000] [1045] 
Access denied for user 'roott'@'localhost' 
(using password: NO)

Example 1: Suppose we have a database “gfg” with a table “students”. We want to fetch the details like “id” and name of all the students present in the table “students”.




<?php
    $dsn = "mysql:host=localhost;dbname=gfg";
    $user = "root";
    $passwd = "";
    $pdo = new PDO($dsn, $user, $passwd);
    $stm = $pdo->query("SELECT * FROM students");
    $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
    foreach($rows as $row) {
        printf("{$row['id']} {$row['name']}\n");
    }
?>

Output:

1  Student1
2  Student2
3  Student3
4  Student4

Example 2: In this case, suppose we have the “employee” table in “gfg” database. We want to delete the employee from the “employee” table whose “id = 123”. 




<?php
    try {
        $dbhost = 'localhost';
        $dbname='gfg';
        $dbuser = 'root';
        $dbpass = '';
        $connect = new PDO(
"mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    }
   catch (PDOException $e) {
        echo "Error : " . $e->getMessage() . "<br/>";
        die();
    }
    $count = $connect->exec("DELETE FROM employee WHERE emp_id = 123 ");
    echo("Number of rows to be deleted from the employee table is: ". $count);
?>

Output:

The number of rows to be deleted from the employee table is: 1

Benefits of PDO:

Supported Database: PDO supports 12 different databases.


Article Tags :