MySQL DELETE

From JumbaWiki

Jump to: navigation, search

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.

PHP

//This example shows how to delete a record from a databse by a unique field (in this case 'id').
 
//Variables:
$id = 1;
 
//MySQL string:
$sql = "DELETE FROM table_name WHERE id='$id'";
 
$result = mysql_query($sql);
 
//Output a result
if($result){
	echo "Record deleted from the database!";
} else {
	$message = "Failed to delete! This is the MySQL Error output:<br>" + mysql_error();
}

ASP


See also

MySQL Basics
Connect/Disconnect | Select | Insert | Update | Delete
Personal tools
Australian Photographs | Unique Views of Reality | Daedalus