Home

PDO

PDO, or PHP Data Objects, is a consistent, object-oriented data access layer in PHP. It provides a lightweight, database-agnostic interface for performing common database operations through a uniform API, while delegating database-specific details to individual drivers. The goal is to allow applications to switch database systems with minimal code changes.

PDO relies on drivers that implement the extension for each database (for example, PDO_MYSQL, PDO_PGSQL, PDO_SQLITE).

Key features include prepared statements, which help prevent SQL injection; transactions with commit and rollback; a

Error handling is typically configured to throw exceptions (PDO::ERRMODE_EXCEPTION) to enable try-catch blocks. PDO does not

Use of PDO is common in PHP applications that require database portability or a clean separation between

A
connection
is
created
with
a
DSN
string,
a
username,
and
a
password,
plus
optional
driver
options.
Example
DSN:
mysql:host=localhost;dbname=testdb;charset=utf8mb4.
Once
connected,
developers
can
use
prepared
statements,
named
or
positional
placeholders,
and
bound
parameters.
choice
of
fetch
modes
(e.g.,
FETCH_ASSOC,
FETCH_NUM,
FETCH_OBJ)
and
behavior
for
default
results;
and
both
named
and
positional
parameter
binding.
PDO
also
supports
persistent
connections
through
options
like
ATTR_PERSISTENT
and
control
over
emulated
prepared
statements
via
ATTR_EMULATE_PREPARES.
implement
a
database
driver
itself;
it
provides
a
uniform
API
while
the
actual
query
execution
is
handled
by
the
underlying
database
driver.
While
it
supports
many
common
features
across
drivers,
some
database-specific
capabilities
may
require
native
SQL
or
driver
functions.
SQL
and
application
logic.
It
is
not
a
database
itself,
but
a
consistent
interface
designed
to
simplify
switching
backends
and
to
improve
security
and
maintainability.