PHP MySQL Delete From

The DELETE statement is used to delete rows from a database table.

Deleting data from the database

The DELETE FROM statement is used to delete records from a database table.

Syntax

DELETE FROM table_name
WHERE column_name = some_value

Note:SQL is case-insensitive. DELETE FROM is equivalent to delete from.

To make PHP execute the above statement, we must use the mysql_query() function. This function is used to send queries and commands to the SQL connection.

Example

Earlier in this tutorial, we created a table named 'Persons'. It looks something like this:

FirstName LastName Age
Peter Griffin 35
Glenn Quagmire 33

The following example deletes all records with LastName='Griffin' from the 'Persons' table:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("my_db", $con);
mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");
mysql_close($con);
?>

After this deletion, the table looks like this:

FirstName LastName Age
Glenn Quagmire 33