PDO provides exception-driven database access through a MySQL DSN and prepared statements.
The examples focus on the operation itself, including the important distinction: MySQLi is another MySQL-specific option; PDO is driver-based.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Before You Start
The examples use PHP 8.2+ features where shown. Database snippets require the corresponding PDO driver, demo tables, and credentials configured outside source code.
Example 1: Basic Use
Connecting is only the first step: use bound parameters for later queries.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$stmt = $pdo->prepare("SELECT email FROM users WHERE active = :active ORDER BY id LIMIT 5");
$stmt->execute(["active" => 1]);
echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
Connecting is only the first step: use bound parameters for later queries. Named bound filters can be combined after connecting.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$stmt = $pdo->prepare("SELECT email FROM users WHERE active = :active AND id > :min ORDER BY id LIMIT 5");
$stmt->execute(["active" => 1, "min" => 10]);
echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
Connecting is only the first step: use bound parameters for later queries. Named bound filters can be combined after connecting.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$stmt = $pdo->prepare("SELECT email FROM users WHERE active = :active AND id > :min ORDER BY id LIMIT 5");
$stmt->execute(["active" => 1, "min" => 20]);
echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Important Behavior and Edge Cases
Do not embed production passwords directly in scripts or interpolate user input into SQL.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| Connecting to MySQL with PDO | PDO provides exception-driven database access through a MySQL DSN and prepared statements. |
| Related API or alternative | MySQLi is another MySQL-specific option; PDO is driver-based. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Do not embed production passwords directly in scripts or interpolate user input into SQL. |
| Confusing it with a related API | MySQLi is another MySQL-specific option; PDO is driver-based. |
Key Takeaways
- PDO provides exception-driven database access through a MySQL DSN and prepared statements.
- Do not embed production passwords directly in scripts or interpolate user input into SQL.
- MySQLi is another MySQL-specific option; PDO is driver-based.
Use Connecting to MySQL with PDO when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.