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
एक 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
// 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
कितनी 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
$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
बिना 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
$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 एक 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
// 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
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
$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
- बिना WHERE clause के DELETE FROM table चलाना, जो table की हर single row delete कर देता है -- अक्सर intended की exact opposite।
- एक DELETE का WHERE clause unvalidated, unbound user input से बनाना, SQL injection और गलत rows delete होने दोनों का risk लेते हुए।
- यह मान लेना कि एक 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 करने के लिए उपयोगी।
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