PHP PDO Introduction
In this page:
What is PDO?
PDO (PHP Data Objects) provides a single, consistent object-oriented API for talking to many different database systems — MySQL, PostgreSQL, SQLite, and more — so switching databases later doesn't mean rewriting all your query code.
Example: What is PDO?
<?php
$pdo = new PDO('sqlite::memory:');
echo get_class($pdo);
// The same PDO API works for MySQL, PostgreSQL, SQLite, and more
?>
Login to try C/C++/Java/PHP code in the editor
PDO Error Modes
You create a PDO connection with new PDO($dsn, $user, $password), where the DSN (Data Source Name) string specifies the database driver and connection details, like 'mysql:host=localhost;dbname=myapp'.
Example: PDO Error Modes
<?php
// On a real server: new PDO('mysql:host=localhost;dbname=myapp', $user, $password);
$pdo = new PDO('sqlite::memory:');
echo "Connected via DSN";
?>
Login to try C/C++/Java/PHP code in the editor
Executing Queries (query)
PDO supports named placeholders (:name) and positional placeholders (?) in prepared statements, giving you flexibility in how you bind values compared to mysqli's more rigid parameter binding.
Example: Executing Queries (query)
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id INTEGER, name TEXT)");
$stmt = $pdo->prepare("INSERT INTO users (id, name) VALUES (:id, :name)");
$stmt->execute(['id' => 1, 'name' => 'Alice']);
echo "Inserted using named placeholder";
?>
Login to try C/C++/Java/PHP code in the editor
Executing Statements (exec)
Setting PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION makes PDO throw real exceptions on database errors instead of silently returning false, which is strongly recommended since it makes failures impossible to accidentally ignore.
Example: Executing Statements (exec)
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$pdo->exec("SELECT * FROM nonexistent_table");
} catch (PDOException $e) {
echo "Caught exception instead of a silent failure";
}
?>
Login to try C/C++/Java/PHP code in the editor
Closing PDO Connections
Because PDO abstracts over multiple database engines, many teams choose it over mysqli by default even for MySQL-only projects, simply for its more consistent and modern API and easier future portability.
Example: Closing PDO Connections
<?php
$pdo = new PDO('sqlite::memory:');
echo "PDO's consistent API is why many teams pick it even for MySQL-only projects";
$pdo = null; // closes the connection
?>
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- PHP MySQL Introduction
- PHP MySQLi Connection
- PHP PDO Introduction
- PHP CRUD Operations
- PHP Prepared Statements
- PHP Stored Procedures
- PHP Transactions
- PHP Error Handling in DB
- PHP MySQL Connect
- PHP MySQL Create DB
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Get Last ID
- PHP MySQL Insert Multiple
- PHP MySQL Prepared Statements
- PHP MySQL Select Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Delete Data
- PHP MySQL Update Data
- PHP MySQL Limit Data