PHP PDO

PHP PDOPHP PDO tutorial introduces you to one of the most important PHP extensions called PHP Data Objects or PDO, which was available since PHP version 5.0.

PHP Data Objects or PDO is a PHP5 extension that provides a lightweight relational database management system (RDMBS) connection abstract library. PDO provides a unified interface for working with various RDBMS including creating database connection, performing queries, handling error, etc.

PDO relies on database specific drivers e.g., PDO_MYSQL for MySQL, PDO_PGSQL for PostgreSQL, PDO_OCI for Oracle, etc., to function properly so to use PDO for a specific database, you need to have the corresponding database driver available.

There are several libraries that provide the same functionality as PDO such as PEAR DB package and ADOdb library. However, those libraries were written in PHP, while PDO was developed in C/C++ that makes it perform faster.

In addition, PDO does not require you to include script files in your application like other libraries, which helps distribute and install the application easier and faster.

PDO Quick Start

This section shows you step by step how to connect to some relational database management systems including MySQL, PostgreSQL and Oracle.

  • Connecting to MySQL – guides you step-by-step how to connect to MySQL database using PDO.
  • Connecting to PostgreSQL – shows you how to connect to PostgreSQL database using PDO.
  • Connecting to Oracle – connects to Oracle database using PDO.

PDO Basic Operations

This section illustrates some common operations that you often work with databases including:

  • Creating new tables – shows you how to use PDO API to create tables for the employees sample database.
  • Querying data with different fetch modes – guides you how to query data from database tables with different fetch modes.
  • Inserting data into tables – walks you through the steps of inserting data into database tables.
  • Updating data – shows you how to update data in the database tables using PDO API.
  • Deleting data – guides you how to delete data in the database table using PDO API.

PDO Prepared Statements

In this section, we’ll introduce you to prepared statement and show you the advantages of PDO prepared statements. We also show how to use the prepared statements to make your application more secure e.g., to avoid SQL injection.

  • Positional and named holders.
  • Prepared statements and bound values.
  • CRUD using prepared statements.
  • Working with BLOBs.

PDO & Stored Procedures

This section shows you some examples of dealing with stored procedures including:

  • Calling stored procedures.
  • Getting values returned from the stored procedures.

PDO Transaction

PDO gives you some handy methods including PDO::beginTransaction(), PDO::commit() and PDO::rollBack() that handles transactions effectively. This section gives you an example of handling transactions in your web application.

  • PDO handling transaction.

PDO Reference