Introduction to PHP PDO
PHP can access a large range of relational database management systems such
as MYSQL, SQLite, and PostgreSQL. The PHP 5.1 version offered a new
database connection abstraction library, which is PHP Data Objects (PDO).
What is PDO?
PDO refers to PHP Data Object, which is a PHP extension that defines a
lightweight and consistent interface for accessing a database in PHP. It is a set
of PHP extensions which provide a core PDO class and database-specific driver.
Each database driver can expose database-specific features as a regular
extension function that implements the PDO interface.
PDO mainly focuses on data access abstraction rather than database abstraction.
It provides data-access abstraction layer, which means, regardless of which
database we are using, we have to use the same functions provided by that
database to issue queries and fetch data. PDO does not provide data abstraction,
as it does not rewrite the SQL or emulate missing features.
Advantage of PDO
PDO provides various ways to work with objects and retrieves prepared
statements that make work much easier. It is a database access tool in PHP
through which we enable uniform access across several databases.
PDO allows comparatively seamless switching between different databases and
platforms, which can be easily done by changing the connection string. It does
not support database-specific syntaxes.
There are some advantages of PDO as follows:
o Database support
The PDO extension can access any database which is written for PDO
driver. There are several PDO drivers available which are used
for FreeTDS, Microsoft SQL Server, Sybase, IBM DB2, Oracle Call
Interface, Firebird/Interbase 6, and PostgreSQL databases, among
many more.
The drivers are not available in every system automatically, so we have to
find our available drivers and add ones when we need them.
o Database connecting
There are different syntaxes available to establish the database
connection. These syntaxes depend on specific databases. While using
PDO, operations must be wrapped in try/catch blocks and utilize the
exception technique.
Usually, only a single connection needs to create, and these connections
are closed by programming the database to set as a null.
o Error handling
PDO permits to use exceptions for error handling. To produce an
exception, PDO can be forced into a relevant error mode attribute.
There are three error modes, i.e., Silent (default), Warning,
and Exception. Warning and Exception are more useful in DRY
programming.
a. Silent - It is a default error mode.
b. Warning - It is useful for debugging.
c. Exception - This mode allows graceful error handling while hiding
data that a person might use to exploit your system.
o Insert and Update
PDO reduces the commonly used insert and update database operation
into a two-step process, i.e.
Prepare >> [Bind] >> Execute.
Through this method, we can take full advantage of PDO's prepared
statements, which protect against malicious attacks through SQL
injection.
Prepared statements are pre-compiled SQL statements that can be
executed multiple times by sending this data to the server. This data,
which is used within the placeholder, is automatically protected from the
SQL injection attack.
Benefits of using PDO
PDO is the native database driver. There are some benefits of using PDO that
are given below:
o Usability - It contains many helper functions to operate automatic
routine operations.
o Reusability - It offers the unified API to access multiple databases.
o Security - It uses a prepared statement which protects from SQL
injection. A prepared statement is a pre-compiled SQL statement that
separates the instruction of the SQL statement from the data.
PDO Classes
There are three PDO classes, which are given below:
o PDO - It represents a connection between PHP and the database.
o PDOStatement - It represents the prepared statement and after the
execution of the statement, sets an associated result.
o PDOException - It represents errors raised by PDO.
Databases supported by PDO
1. MySQL
2. PostgreSQL
3. Oracle
4. Firebird
5. MS SQL Server
6. Sybase
7. Informix
8. IBM
9. FreeTDS
[Link]
[Link]
12.4D