The main syntax of a delete request - Let’s take a look at the following table" Data "with four columns" ID "," FirstName "," LastName "and" Age ". You can use the following code to delete a record of person ID 201 from the Data table.Delete a query using a procedural method:
php
$
link
=
mysql i_connect
("localhost", "root", "", "Mydb");
if ($link === false) {
die ("ERROR: Could not connect.". mysqli_connect_error());
}
$
sql
=
"DELETE FROM Data WHERE ID = 201 "
;
if (mysqli_query ($link, $sql)) {
echo "Record was deleted successfully.";
}
else {
echo "ERROR: Could not be able to execute $sql."
. mysqli_error ($link);
}
mysqli_close ($link);
?>
Output: Table after update - Web browser output: Delete request using object-oriented method:
php
$
mysqli
=
new
mysqli ("localhost", "root", " "," Mydb ");
if ($mysqli === false) {
die ("ERROR: Could not connect.". $mysqli-> connect_error);
}
$sql = "DELETE FROM Data WHERE ID = 201";
if ($mysqli-> query ($sql) === true) {
echo" Record was deleted successfully. ";
} else {
echo "ERROR: Could not be able to execute $sql."
. $mysqli-> error;
}
$mysqli-> close();
?>
Output: Table after update - Web browser output: Delete request using PDO method:
php
try {
$
pdo
=
new
PDO ("
mysql: host
=
localhost
;
dbname
=
Mydb
"," root "," ");
$pdo-> setAttribute (PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die ("ERROR: Could not connect.". $e-> getMessage());
}
try {
$sql = "DELETE FROM Data WHERE ID = 201";
$pdo-> exec ($sql);
echo "Record was deleted successfully. ";
} catch (PDOException $e) {
die ("ERROR: Could not be able to execute $sql."
. $e-> getMessage());
}
unset ($pdo);
?>
Output: Table after update - Web browser output: