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:
As with all actions relating to a MySQL Databse, we have to use the mysql_query() function.
mysql_query("delete from table_name where name='$name' limit 1");
Anything between the quote marks is what we want to do.
This tells the database that we're deleting a record(s)
This tells the database which table we'll find the record to delete.
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.
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!