Delete selected database rows with a prepared DELETE statement.
The examples focus on the operation itself, including the important distinction: UPDATE changes rows; DELETE removes them.
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
Authorize the deletion first; WHERE on a unique key limits its scope.
<?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]
);
$recordId = 3;
$stmt = $pdo->prepare("DELETE FROM users WHERE id = :id");
$stmt->execute(["id" => $recordId]);
echo $stmt->rowCount() === 1 ? "deleted" : "not found";
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
Authorize the deletion first; WHERE on a unique key limits its scope.
<?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]
);
$recordId = 8;
$stmt = $pdo->prepare("DELETE FROM users WHERE id = :id");
$stmt->execute(["id" => $recordId]);
echo $stmt->rowCount() === 1 ? "deleted" : "not found";
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
Authorize the deletion first; WHERE on a unique key limits its scope.
<?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]
);
$recordId = 21;
$stmt = $pdo->prepare("DELETE FROM users WHERE id = :id");
$stmt->execute(["id" => $recordId]);
echo $stmt->rowCount() === 1 ? "deleted" : "not found";
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
Require explicit WHERE conditions and restrict authorization before executing mutations.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| Deleting Database Records with PDO | Delete selected database rows with a prepared DELETE statement. |
| Related API or alternative | UPDATE changes rows; DELETE removes them. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Require explicit WHERE conditions and restrict authorization before executing mutations. |
| Confusing it with a related API | UPDATE changes rows; DELETE removes them. |
Key Takeaways
- Delete selected database rows with a prepared DELETE statement.
- Require explicit WHERE conditions and restrict authorization before executing mutations.
- UPDATE changes rows; DELETE removes them.
Use Deleting Database Records with PDO when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.