Delete Data From a MySQL Table in PHP

 Delete Data From a MySQL Table in PHP 

The DELETE statement is used to delete records from a table:

DELETE FROM table_name WHERE some_column = some_value  

Notice:- The WHERE clause specifies which record or records that should be deleted. If omit the WHERE clause, all records will be deleted!

 "MyGuests" table:

id

firstname

lastname

email

reg_date

1

John

Doe

john@example.com

2014-10-22 14:26:15

2

Mary

Moe

mary@example.com

2014-10-23 10:22:30

3

Julie

Dooley

julie@example.com

2014-10-26 10:48:23

 Examples:- Delete the record with id=3 in the "MyGuests" table:

 Example (MySQLi Procedural)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";

if (mysqli_query($conn, $sql)) {
  echo "Record deleted successfully";
} else {
  echo "Error deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>
 

Output:-

id

firstname

lastname

email

reg_date

1

John

Doe

john@example.com

2014-10-22 14:26:15

2

Mary

Moe

mary@example.com

2014-10-23 10:22:30

=============

Post a Comment

0 Comments