Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

MySQL Delete Data

PHP

MySQL Delete Data

DELETE statement MySQL table से unwanted या unnecessary records को हटाने के लिए उपयोग किया जाता है। PHP में हम MySQLi (Procedural) और PDO दोनों तरीकों से records delete कर सकते हैं। लेकिन DELETE query बेहद संवेदनशील होती है—गलत WHERE condition या गलत SQL चलाने पर पूरा table empty हो सकता है। इसलिए DELETE हमेशा सावधानी के साथ चलाएँ।

  • DELETE FROM table_name WHERE condition;
  • WHERE भूलने पर पूरा data delete हो जाएगा — यह सबसे बड़ी beginner mistake है।
  • DELETE query चलाने से पहले हमेशा SELECT के साथ verify करें कि कौन से rows delete होंगे।

DELETE का Basic Syntax

Basic SQL: DELETE
DELETE FROM students WHERE id = 5;
-- सिर्फ id = 5 वाला record delete होगा

DELETE FROM students;
-- Warning: पूरा table delete हो जाएगा

Warning & Safety Tips

  • Warning: WHERE clause लिखना कभी न भूलें।
  • Suggestion: DELETE से पहले SELECT query चलाकर verify जरूर करें।
  • Note: बड़े applications में DELETE की जगह “soft delete” (status=0) का उपयोग किया जाता है ताकि data वापस लाया जा सके।
  • Logs रखें कि कौन सा record कब delete हुआ (audit trail systems में useful)।
  • Error handling और transaction का उपयोग बड़े deletes में ज़रूरी है।

MySQLi Procedural Example (Simple Delete)

यह उदाहरण दिखाता है कि id के आधार पर record कैसे delete किया जाता है:

उदाहरण: MySQLi Procedural (DELETE)
 <?php
  $conn = mysqli_connect("localhost", "root", "", "school");
  $sql = "DELETE FROM students WHERE id = 3";
  if (mysqli_query($conn, $sql)) {
    echo "Record Deleted Successfully";
  } else {
    echo "Delete Error: " . mysqli_error($conn);
  }
  mysqli_close($conn);
  ?>

MySQLi Procedural (Prepared Statement) — Recommended

Prepared statements SQL Injection से बचाते हैं और हमेशा सुरक्षित method माने जाते हैं।

उदाहरण: MySQLi Prepared DELETE
 <?php
  $conn = mysqli_connect("localhost", "root", "", "school");
  $stmt = mysqli_prepare($conn, "DELETE FROM students WHERE id = ?");
  mysqli_stmt_bind_param($stmt, "i", $id);
  $id = 4;
  mysqli_stmt_execute($stmt);
  echo "Record Deleted with Prepared Statement";
  mysqli_stmt_close($stmt);
  mysqli_close($conn);
  ?>

PDO Delete Example

PDO में DELETE query prepared statements के साथ execute की जाती है ताकि query सुरक्षित रहे और error handling आसान हो।

उदाहरण: PDO (DELETE)
  <?php
  $pdo = new PDO("mysql:host=localhost;dbname=school", "root", "");
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $stmt = $pdo->prepare("DELETE FROM students WHERE id = ?");
  $stmt->execute([5]);
  echo "Record Deleted Successfully";
  ?>

Additional Examples

1️. Multiple Conditions DELETE

कभी-कभी हमें multiple conditions पर delete करना पड़ता है:

उदाहरण: Multiple Conditions
DELETE FROM students WHERE class = 10 AND city = 'Delhi'

2️. Delete with LIMIT

LIMIT के साथ delete rare cases में उपयोग होता है (bulk cleanups):

उदाहरण: DELETE LIMIT
DELETE FROM logs ORDER BY id ASC LIMIT 100

Best Practices

  • DELETE से पहले data backup लें (खासकर production server पर)।
  • Soft Delete system उपयोग करें: UPDATE users SET status = 0 WHERE id = 5;
  • Always use prepared statements with user input.
  • Bulk delete tasks में LIMIT + ORDER BY आवश्यक है।

Common Mistakes (और समाधान)

  • गलती: WHERE भूल जाना → पूरा table delete
    Solution: हमेशा पहले SELECT से verify करें।
  • गलती: गलत column name
    Solution: DESC table_name; चलाकर structure देखें।
  • गलती: NULL values handle न करना
    Solution: WHERE conditions में NULL को IS NULL से compare करें।

Conclusion

DELETE powerful है लेकिन risk के साथ आता है। हमेशा WHERE condition verify करें, prepared statements का उपयोग करें, और आवश्यकता होने पर soft delete implement करें। सही तरीके से delete करना database की health के लिए महत्वपूर्ण है।

Article By: Brajlal Prasad
Created on: 05 Nov 2025  11  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]

Financial Calculator

Financial Calculator

Take control of your finances with our powerful Financial Calculator app—calculate loans, SIP, PPF, NPS and more all in one place!

Play Store