PHP Introduction | Database Access with PDO | What is PDO for Database Access?

Web applications handle many kinds of data. For that reason, data is often managed with a database. There are many types of databases, but the most common and widely used type is the “relational database.”

SQL is a dedicated language used to query relational databases and request the information needed from them. MySQL, Oracle, PostgreSQL, and SQLite, which has recently been used as an internal database on Android and iPhone, are all types of relational databases. There are of course many other databases as well, but at this stage it is useful to think of “database = relational database.”

Three Ways to Use Databases

PHP includes several features for using relational databases. Broadly speaking, they can be grouped into the following three approaches.

Using Functions

For example, with MySQL, functions for database access are provided in forms such as mysql_OO. You use them by calling these functions. This approach has existed since the early days of PHP and can be used with versions before PHP 5. However, calling every function directly is far removed from modern object-oriented programming. In other words, it is the style used in older PHP versions before PHP 5.

Using Objects

For example, with MySQL, you can create a MySQL access object with something like new mysqli and call the methods inside it. This organizes the functions above into an object, allowing database access in an object-oriented way.

Using PDO

PDO stands for “PHP Data Objects.” You create an object of the PDO class and use the database by calling its methods. The major difference from the other approaches is that “all databases can be handled in the same way.” By setting the database type and related information when creating the object, you can use it afterward without worrying about the database type.

 

This section explains “access using PDO.” PDO is a feature newly provided in PHP 5.1. It does more than simply provide an object-oriented mechanism; it is designed to absorb database differences so you can create processing that does not change regardless of the access target. If you are learning something new from here, PDO is the best choice.