One of the most compelling things PHP has going for it is it support for a wide variety of databases. And this week, PHP 101 is going to take advantage of that database support to create dynamic data-driven Web sites. This primer covers different techniques to select, insert and delete records, together with some tips to track and squash bugs when building SQL-driven sites.
Finally, now that you've added a million users, it's time to do a little routine maintenance. This example demonstrates how to use the DELETE statement to wipe out a user and his list of sites. Again, the basic principles remain the same, with only the query string changing.
<?php
// has the form been submitted?
if($submit)
{
// connect to the database
$conn
= mysql_connect("localhost", "test", "test");
mysql_select_db("php101",$conn);
$result
= mysql_query("select
title1,url1,title2,url2,title3,url3 from
url_list where
uid = '$username'",$conn);
// check username
$num_rows = mysql_num_rows($result);
//
if no rows are returned, the username is invalid.
if(!$num_rows)
{
?>
<html>
<head>
<basefont
face=Arial>
</head>
<body>
<center>
<font size="3">User not found</font><br>
<a
href="nuke.php4">Click here to try again</a>
</center>
</body>
</html>
<?php
}
else
{
// if username is valid, delete the bookmarks.
$result
= mysql_query("delete from url_list where uid =
'$username'",$conn);
?>
<html>
<head>
<basefont
face=Arial>
</head>
<body>
<center>
<h3>Success!</h3>
<?echo
$username; ?>'s book marks have been deleted!
</center>
</body>
</html>
<?php
}
}
else
{
//
if form has not been submitted
?>
<html>
<body>
<form action="nuke.php4"
method="POST">
<table>
<tr>
<td>
Username
</td>
<td>
<input
type="text" name="username" length=10 maxlength="30">
</td>
</tr>
<tr>
<td
colspan="2" align="center">
<input type="submit" name="submit" value="Delete
Bookmarks">
</td>
</tr>
</table>
</form>
</body>
</html>
<?
}
?>