Delete Data From A MySQL Table Using MySQLi and PDO
Delete Data From A MySQL Table Using MySQLi and PDO
Notice the WHERE clause in the DELETE syntax: The WHERE clause specifies whic
that should be deleted. If you omit the WHERE clause, all records will be deleted!
The following examples delete the record with id=3 in the "MyGuests" table:
$conn->close();
?>
// Create connection
$conn = mysqli_connect($servername, $username, $password,
$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Example (PDO)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname",
$username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$conn = null;
?>
After the record is deleted, the table will look like this: