← Back to PHP Course | Chapter 11: Database | Lesson 19 of 21

PHP MySQL Delete Data

किसी table से एक row permanently हटाना -- एक cancelled order, एक deleted comment, एक expired session -- एक DELETE statement से किया जाता है। क्योंकि DELETE data पूरी तरह हटा देता है, बिना किसी built-in undo के, यह उन SQL statements में से एक है जिसे सबसे ज़्यादा care चाहिए exactly किस rows को एक WHERE clause actually target करता है इसे लेकर।
Syntax
php
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

एक WHERE Clause के साथ Basic DELETE

DELETE FROM tableName WHERE condition सिर्फ उस condition से match करने वाली rows हटाता है, बाकी हर row को पूरी तरह untouched छोड़ते हुए -- DELETE FROM users WHERE id = 5 exactly एक specific user को हटाता है, और नहीं।

उदाहरण: Basic DELETE with a WHERE Clause

php
<?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";
?>

कितनी Rows Delete हुईं Check करना

mysqli_affected_rows($conn) सबसे हाल की query द्वारा actually modify की गई rows की संख्या return करता है -- एक DELETE के लिए, यह exactly confirm करता है कि कितनी rows हटाई गईं, जो यह detect करने के लिए उपयोगी है कि कोई delete zero rows से match हुआ (शायद ID कभी exist ही नहीं की)।

उदाहरण: Checking How Many Rows Were Deleted

php
<?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
?>

बिना WHERE के Delete करने का Danger

बिल्कुल कोई WHERE clause न वाला DELETE FROM tableName पूरी तरह valid SQL है, लेकिन यह table की हर single row delete कर देता है -- गलती से चलने पर एक devastating mistake, क्योंकि कोई built-in confirmation step या undo नहीं है।

उदाहरण: The Danger of Deleting Without WHERE

php
<?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)";
?>

Soft Deletes एक Safer Alternative की तरह

DELETE से permanently एक row हटाने के बजाय, एक "soft delete" row को deleted mark करने के लिए एक flag column (जैसे deleted_at या is_deleted) set करता है साथ ही इसे table में रखते हुए -- app में कहीं और की queries फिर soft-deleted rows को filter out कर देती हैं, लेकिन ज़रूरत पड़ने पर data recoverable रहता है।

उदाहरण: Soft Deletes as a Safer Alternative

php
<?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";
?>

Related Rows को साथ Delete करना

जब delete की जा रही row की किसी दूसरे table में related rows हों (जैसे एक order और उसके order items), उन related rows को भी handle करना ज़रूरी है -- या तो पहले से explicitly delete करके, या database level पर ON DELETE CASCADE से configured एक foreign key के through automatically।

उदाहरण: Deleting Related Rows Together

php
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बिना WHERE clause के DELETE FROM table चलाना, जो table की हर single row delete कर देता है -- अक्सर intended की exact opposite।
  2. एक DELETE का WHERE clause unvalidated, unbound user input से बनाना, SQL injection और गलत rows delete होने दोनों का risk लेते हुए।
  3. यह मान लेना कि एक deleted row बाद में recover की जा सकती है -- बिना एक backup या एक "soft delete" flag column के, एक DELETE permanent है।
चैप्टर सारांश
  • DELETE FROM table WHERE condition सिर्फ उस condition से match करने वाली rows हटाता है।
  • बिल्कुल कोई WHERE clause न वाला DELETE FROM table table की हर row हटा देता है -- कोई चलाने से पहले हमेशा double-check करें कि एक WHERE clause मौजूद है।
  • mysqli_affected_rows($conn) report करता है कि एक DELETE statement ने actually कितनी rows हटाईं, expected संख्या delete हुई यह confirm करने के लिए उपयोगी।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.