13- MySQL Delete Data

0
18

MySQL Delete Data is used In Deleting data using MySQL the DELETE Statement is used.

Syntax:

DELETE FROM tablename
WHERE specificcolumn = correspondingvalue

MySQL Delete Data

 

Take Note! Using the DELETE command you must not forget to include the WHERE clause because the Where clause is used to specify record you want to delete. Without having the WHERE clause, all records in the table will be deleted.

Here the following examples in Deleting record from “tblpeople”.

Example using MySQLi(Object-Oriented):

  1. die("Connection failed: " . $conn->connect_error);
  2. }
  3.  
  4. // sql to delete record from table
  5. $sql = "DELETE FROM tblpeople WHERE ID=9";
  6.  
  7. if ($conn->query($sql) === TRUE) {
  8. echo "Record Delete successfully";
  9. } else {
  10. echo "Error: " . $sql . "
  11. " . $conn->error;
  12. }
  13.  
  14. $conn->close();
  15. ?>

Example using MySQLi(Procedural):

  1. $username = "root";
  2. $password = "";
  3. $database = "mysqltutorial";
  4.  
  5. // Create connection
  6. $conn = mysqli_connect($server, $username, $password, $database);
  7. // Check connection
  8. if (!$conn) {
  9. die("Connection failed: " . mysqli_connect_error());
  10. }
  11.  
  12. // sql to Delete record from table
  13. $sql = "DELETE FROM tblpeople WHERE ID=9";
  14.  
  15. if (mysqli_query($conn, $sql)) {
  16.  
  17. echo "Record Delete successfully";
  18. } else {
  19. echo "Error: " . $sql . "
  20. " . mysqli_error($conn);
  21. }
  22.  
  23. mysqli_close($conn);
  24. ?>

Example using PDO:

  1. $sql = "DELETE FROM tblpeople WHERE ID=8";
  2.  
  3. $conn->exec($sql);
  4. echo "Record Delete successfully";
  5. }
  6. catch(PDOException $e)
  7. {
  8.  
  9. echo $sql . "
  10. " . $e->getMessage();
  11. }
  12.  
  13. $conn = null;
  14. ?>

Our Previous Topic Called: 12- MySQL Select Data

Facebook Comments
(Visited 27 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here