PHP PDO

PHP PDO is a database access layer that provides a uniform interface for working with multiple databases.

PDO simplifies the common database operations including:

  • Creating database connections
  • Executing queries using prepared statements
  • Calling stored procedures
  • Performing transactions
  • And handling errors

PDO allows you to work with any database that has a PDO driver available. PDO relies on database-specific drivers, e.g., PDO_MYSQL for MySQL, PDO_PGSQL for PostgreSQL, PDO_OCI for Oracle database, etc., to function properly. Here’s the complete list of PDO drivers.

Therefore, to use PDO for a specific database, you need to have a corresponding database driver available.

The following diagram illustrates how PDO works:

php pdo

PDO makes it easy to deploy PHP applications because it doesn’t require you to manually include any script files in your application like other libraries.

Section 1. PDO Quick Start

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

Section 2. Creating a sample database

  • Create a sample database – show you how to create a sample database and tables in the MySQL database server and a reusable script for connecting to the database.
  • Creating new tables – learn how to create a new table in a MySQL database from PHP using PDO.

Section 3. CRUD in PDO

This section illustrates the common database operation including creating (insert), reading(select), updating, and deleting data using PDO. These basic operations are often called CRUD.

Section 4. Calling Stored Procedures

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

Section 5. Managing Transaction in PDO

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

  • PDO Transaction – learn how to perform a database transaction from PHP using PDO.

Section 6. PDO References