PHP MySQL Delete Data
In this page:
DELETE FROM table_name WHERE condition; // without WHERE, every row is deleted
mysqli_query($conn, $sql);
mysqli_affected_rows($conn); // how many rows were deleted
Basic DELETE with a WHERE Clause
DELETE FROM tableName WHERE condition removes only the rows matching that condition, leaving every other row completely untouched -- DELETE FROM users WHERE id = 5 removes exactly one specific user, no more.
Example: Basic DELETE with a WHERE Clause
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER, name TEXT)");
$db->exec("INSERT INTO users VALUES (5, 'Alice')");
$db->exec("DELETE FROM users WHERE id = 5");
// Print "Row removed" to the output
echo "Row removed";
?>
Login to try C/C++/Java/PHP code in the editor
Checking How Many Rows Were Deleted
mysqli_affected_rows($conn) returns the number of rows the most recent query actually modified -- for a DELETE, this confirms exactly how many rows were removed, which is useful for detecting when a delete matched zero rows (perhaps the ID never existed).
Example: Checking How Many Rows Were Deleted
<?php
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER)");
$db->exec("INSERT INTO users VALUES (1), (2)");
$db->exec("DELETE FROM users WHERE id = 1");
echo $db->changes() . " row(s) deleted"; // mysqli_affected_rows($conn) equivalent
?>
Login to try C/C++/Java/PHP code in the editor
The Danger of Deleting Without WHERE
DELETE FROM tableName with no WHERE clause at all is entirely valid SQL, but it deletes every single row in the table -- a devastating mistake if run by accident, since there is no built-in confirmation step or undo.
Example: The Danger of Deleting Without WHERE
<?php
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER)");
$db->exec("INSERT INTO users VALUES (1), (2), (3)");
// DELETE FROM users; -- with no WHERE, this would remove every row!
$db->exec("DELETE FROM users WHERE id = 1");
echo $db->changes() . " row deleted (not all 3)";
?>
Login to try C/C++/Java/PHP code in the editor
Soft Deletes as a Safer Alternative
Instead of permanently removing a row with DELETE, a "soft delete" sets a flag column (like deleted_at or is_deleted) to mark the row as deleted while keeping it in the table -- queries elsewhere in the app then filter out soft-deleted rows, but the data remains recoverable if needed.
Example: Soft Deletes as a Safer Alternative
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER, deleted_at TEXT)");
$db->exec("INSERT INTO users VALUES (1, NULL)");
$db->exec("UPDATE users SET deleted_at = '2024-01-01' WHERE id = 1");
// Print "Row kept, just flagged as deleted" to the output
echo "Row kept, just flagged as deleted";
?>
Login to try C/C++/Java/PHP code in the editor
Deleting Related Rows Together
When a row being deleted has related rows in another table (like an order and its order items), those related rows need handling too -- either deleted explicitly beforehand, or automatically via a foreign key configured with ON DELETE CASCADE at the database level.
Example: Deleting Related Rows Together
<?php
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE orders (id INTEGER)");
$db->exec("CREATE TABLE items (order_id INTEGER)");
$db->exec("INSERT INTO orders VALUES (1)");
$db->exec("INSERT INTO items VALUES (1)");
$db->exec("DELETE FROM items WHERE order_id = 1"); // delete children first
$db->exec("DELETE FROM orders WHERE id = 1");
echo "Both order and its items removed";
?>
Login to try C/C++/Java/PHP code in the editor
- Running DELETE FROM table without a WHERE clause, which deletes every single row in the table -- often the exact opposite of what was intended.
- Building a DELETE's WHERE clause from unvalidated, unbound user input, risking both SQL injection and deleting the wrong rows entirely.
- Assuming a deleted row can be recovered afterward -- without a backup or a "soft delete" flag column, a DELETE is permanent.
- DELETE FROM table WHERE condition removes only the rows matching that condition.
- DELETE FROM table with no WHERE clause at all removes every row in the table -- always double-check a WHERE clause is present before running one.
- mysqli_affected_rows($conn) reports how many rows a DELETE statement actually removed, useful for confirming the expected number were deleted.
DELETE FROM is standard SQL and works identically across all MySQL and MariaDB versions PHP supports.
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