Login | Register | FAQ

Lesson 21: Delete data from database

In the two previous lessons, you have learned to insert and retrieve data from a database. In this lesson, we'll look at how to delete records in the database, which is considerably easier than inserting data.

Delete data using SQL

The syntax for an SQL statement that deletes records is:

	DELETE FROM TableName WHERE condition 


Example: Delete a record

When deleting a record, you can use the unique AutoNumber field in the database. In our database, it is the column named id. Using this unique identifier ensures that you only delete one record. In the next example, we delete the record where id has the value 24:

	<title>Delete data in the database</title>

	// Connect to database server
	mysql_connect("mysql.myhost.com", "user", "sesame") or die (mysql_error ());

	// Select database
	mysql_select_db("mydatabase") or die(mysql_error());

	// The SQL statement that deletes the record
	$strSQL = "DELETE FROM people WHERE id = 24";
	// Close the database connection

	<h1>Record is deleted!</h1>


Remember that there is no "Recycle Bin" when working with databases and PHP. Once you have deleted a record, it is gone and cannot be restored.

Related topics in the PHP ForumRepliesViews
ip is not selected from mysql4191356
Can I send results of 2 MySql queries unto 1 csv file?151661
extracting datetime data from a HTML form161344
How do i save a created mysql database codes?142384
mysql: can't find result of a query (into text file)91125

+ Post a new topic

<< Lesson 20: Retrieve data from a database

Lesson 22: Update data in a database >>