PHP PDO

Created with Sketch.

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:

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. Prepared Statements

Section 4. 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 5. Fetching data

This section discusses various fetch* methods and modes in detail.

  • fetch() – fetch a row from a result set associated with a PDOStatement object.
  • fetchAll() – fetch all rows from a result set object into an array.
  • fetchcolumn() – fetch a single column from the next row in a result set.
  • fetchObject() – fetch the next row from a result set and returns it as an object of a class.
  • PDO::FETCH_KEY_PAIR – select a two-column result in an array where the first column is the key and the second column is the value
  • PDO::FETCH_GROUP – group rows by the unique values of the first column in the result set.
  • PDO::FETCH_CLASS – show you how to return an instance of a class whose properties map to the column values from the database.

Section 6. Calling Stored Procedures

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

Section 6. 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 7. PDO References

Leave a Reply

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