Welcome to S-Design
The home of your bespoke, professional, fast-loading, interactive, database-driven web presence.

MySQL Delete

Once you've got some records in your database, you might want to delete one!
So how do we do that?

Delete a record in a MySQL Database: <?PHP

mysql_query("delete from table_name where name='$name' limit 1");

As with all actions relating to a MySQL Databse, we have to use the mysql_query() function.
Anything between the quote marks is what we want to do.

delete from This tells the database that we're deleting a record(s)

table_name This tells the database which table we'll find the record to delete.

where name='$name' The where clause tells MySQL which record(s) inside table_name to delete. Without this, Mysql would delete all records it finds!!!
In this case, we're deleting any record where the name field matches whatever value is stored in $name.

limit 1 This tells MySQL to delete only one record.
In our example, there should only be one record for each name, so the limit clause isn't really needed. However, if your database has more than one John Smith, and you only want to delete the first one MySQL finds, then limit 1 would do that. However, if you had more than one John Smith and you need to delete a particular one, then you might need more than one condition in the where clause, like so: where name='$name' and address='$address'.
Another advantage of the limit clause... is that it's quicker! Imagine you have a database with 1000 records, and you tell it to delete the record belonging to John Smith, and let's assume there is only one. Without the limit clause, MySQL would scan the database for a record with the name of John Smith and deletes it when it finds it. MySQL would then carry on looking to see if there are any other records for John Smith... and that takes time! Using limit 1 tells MySQL that as soon as it's found & deleted 1 record... to stop looking and finish!