Connecting to a Database with PHP
page 1: Introduction
page 2: Connecting to the Database
page 3: Displaying Tables
page 4: Inserting New Records
page 5: Updating Records
page 6: Deleting Records
Deleting Records
It is a happy day when a pet finds a family to call its own. We then need to delete the animal's record from the database. Now we will use the DELETE FROM statement, using the table and record ID as your arguments. Everything else is pretty much the same, and it should start to look familiar, if not comfortable, to you. Be sure to identify the ID of a record you would like to delete. My record number was 7, but you may have something different you would like to delete from your database.
<?php
$connectID = mysql_connect("127.0.0.1", "root", "yourpasswordherer");
mysql_select_db("animal_rescue", $connectID);
$result = mysql_query("DELETE FROM adoption_list WHERE ID=7",
$connectID);
if ($result == TRUE)
{
print "The record was deleted successfully.<p>";
}
else
{
print "The record could not be deleted.<p>";
}
$resultID = mysql_query("SELECT species, breed, name, age,
personality FROM adoption_list", $connectID);
print "<table border=1><tr><th>Species</th><th>Breed</th>
<th>Name</th><th>Age</th><th>Personality</th></tr>";
while ($row=mysql_fetch_row($resultID))
{
print "<tr>";
foreach ($row as $field)
{
print "<td>$field</td>";
}
print "</tr>";
}
print "</table>";
mysql_close($connectID);
?>
Output to the browser might look something like this:

Are you starting to see that with a few functions, some PHP scripts, and a knowledge of some basic SQL statements you can create a powerful interface to your database in a browser? This will be the key element in creating pages with which users can interact. My next tutorial will build upon this knowledge as we pour our information into forms that give true interactivity. Why don't you go to SQL Course and learn all about the SQL language while you wait?