PHP Delete Data From MySQL shows how to delete data from database. To remove data/records from a single or multiple database tables, MySQL DELETE statement is used.
Syntax
DELETE FROM table [WHERE conditions] [ORDER BY ...] [LIMIT rows]
DELETE FROM clause selects the table in which the records to be deleted.
WHERE clause denotes the rows that has to be deleted.If WHERE condition is satisfied, record is deleted from the table permanently otherwise, all the table records will be deleted.
ROW_COUNT() function is returned by DELETE statement which indicates the number of rows deleted.
Examples
MySQLi (Object-oriented)
[php]
<html>
<body>
<?php
$HostName = "localhost";
$UserName = "root";
$Password = "password";
$dbname="accounts";
// Create connection
$connection = new mysqli($HostName, $UserName, $Password, $dbname);
// Check connection
if ($connection->connect_error)
{
die("Connection failed: " . $connection->connect_error);
}
// sql to delete a record
$sql = "DELETE FROM users2 WHERE id=2";
if ($connection->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $connection->error;
}
$connection->close();
?>
</body>
</html>
[/php]
The output will be "Record deleted successfully" and in phpmyadmin, the output will be like below.