MySQL DELETE Statement

Submitted by:David Villa

Date added:14 May, 2011


This is a general code to use when deleting an item from a MySQL database. This code isn't considered definitive and can be written in many varied ways.

A variation of the code would be not to delete the record (as you tend to always need to restore or retrieve it sooner or later) you can add a 'status' field. If status='d' then it is 'deleted", is status='a' then it is active.

Note: This code requires you to connect to the MySQL database to work. Also, all field and table names need to be set up to match.

Tags: mysql statement , delete statement

Code Snippet:

//This example shows how to delete a record from a databse by a unique field (in this case 'id').

$id = 1;

//MySQL string:
$sql = "DELETE FROM table_name WHERE id='$id'";

$result = mysql_query($sql);

//Output a result
echo "Record deleted from the database!";
} else {
$message = "Failed to delete! This is the MySQL Error output:<br>" + mysql_error();