Content · Web Development · PHP

Deleting Database Records with PDO

Delete selected database rows with a prepared DELETE statement. Require explicit WHERE conditions and restrict authorization before executing mutations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

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


ApproachDifference
Deleting Database Records with PDODelete selected database rows with a prepared DELETE statement.
Related API or alternativeUPDATE changes rows; DELETE removes them.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsRequire explicit WHERE conditions and restrict authorization before executing mutations.
Confusing it with a related APIUPDATE 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.

Share this resourceSend this page to someone who may find it useful.