MySQL - Delete Record
जब row या record को delete करना हो तो DELETE table_name statement के साथ WHERE clause का इस्तेमाल किया जाता है |
DELETE FROM table_name WHERE column_name=some_value
Note : जब DELETE statement WHERE clause के साथ लिखा नहीं जाता तो table के सारे rows delete हो जाते है |
DELETE statement से पहले का table कुछ ऐसे तरह का है |
id | student_name | percentages | grade |
---|---|---|---|
1 | Mukesh | 50.30 | C |
2 | Raju | 68.85 | B |
3 | Akshay | 80 | A |
4 | Raman | 75.25 | A |
5 | Deep | 90.96 | A |
Example for DELETE statement using MySQLi
Source Code :Output :<?php $server = "localhost"; $user = "root"; $password = ""; $db = "info"; $conn = mysqli_connect($server, $user, $password, $db); if($conn){ echo "Connected successfully."; } else{ echo "Connection failed : ".mysqli_connect_error(); } $tb_update = "DELETE FROM student WHERE id=2"; if (mysqli_query($conn, $tb_update)) { echo "Some row deleted successfully."; } else { echo "Row deleting failed : " . mysqli_error($conn); } mysqli_close($conn); ?>
Connected successfully.Some row deleted successfully.
Example for DELETE statement using PDO
Source Code :Output :<?php $server = "localhost"; $user = "root"; $password = ""; $db = "info"; try{ $conn = new PDO("mysql:host=$server;dbname=$db", $user, $password); echo "Connected successfully."; $tb_update = "DELETE FROM student WHERE id=2"; $conn->exec($tb_update); echo "Some row deleted successfully."; } catch(PDOException $e){ echo "Row deleting failed : " . $e->getMessage(); } $conn = null; ?>
Connected successfully.Some row deleted successfully.
DELTE statement के बाद table कुछ ऐसा दिखेगा |
id | student_name | percentages | grade |
---|---|---|---|
1 | Mukesh | 50.30 | C |
3 | Akshay | 80 | A |
4 | Raman | 75.25 | A |
5 | Deep | 90.96 | A |