Create a PDO connection using a mysql: DSN, credentials and safe driver options.
The examples focus on the operation itself, including the important distinction: MySQLi targets MySQL only; PDO supports multiple database drivers.
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
Set charset=utf8mb4 in the DSN; never expose DSN and credentials to browser users.
<?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->query("SELECT 1 AS connected");
echo $stmt->fetchColumn();
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
Set charset=utf8mb4 in the DSN; never expose DSN and credentials to browser users. A successful connection supports both introspection and SQL.
<?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->query("SELECT 1 AS connected");
echo $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
Set charset=utf8mb4 in the DSN; never expose DSN and credentials to browser users. A successful connection supports both introspection and SQL.
<?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->query("SELECT 1 AS connected");
echo json_encode($stmt->fetch(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
Set utf8mb4 in the DSN and avoid printing credentials or connection details in production errors.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| Connecting PHP to MySQL with PDO | Create a PDO connection using a mysql: DSN, credentials and safe driver options. |
| Related API or alternative | MySQLi targets MySQL only; PDO supports multiple database drivers. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Set utf8mb4 in the DSN and avoid printing credentials or connection details in production errors. |
| Confusing it with a related API | MySQLi targets MySQL only; PDO supports multiple database drivers. |
Key Takeaways
- Create a PDO connection using a mysql: DSN, credentials and safe driver options.
- Set utf8mb4 in the DSN and avoid printing credentials or connection details in production errors.
- MySQLi targets MySQL only; PDO supports multiple database drivers.
Use Connecting PHP to MySQL with PDO when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.