March 2002
Intermediate to advanced
592 pages
15h 52m
English
To
introduce querying with user input, we begin by explaining a script
that retrieves the wines made in a wine region that is specified by a
user. This script, shown in Example 5-5, is a
companion to the HTML <form> in Example 5-2.
Example 5-5. A script to display all wineries in a region
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Exploring Wines in a Region</title> </head> <body bgcolor="white"> <?php include 'db.inc'; // Show all wines in a region in a <table> function displayWinesList($connection, $query, $regionName) { // Run the query on the DBMS if (!($result = @ mysql_query ($query, $connection))) showerror( ); // Find out how many rows are available $rowsFound = @ mysql_num_rows($result); // If the query has results ... if ($rowsFound > 0) { // ... print out a header echo "Wines of $regionName<br>"; // and start a <table>. echo "\n<table>\n<tr>" . "\n\t<th>Wine ID</th>" . "\n\t<th>Wine Name</th>" . "\n\t<th>Type</th>" . "\n\t<th>Year</th>" . "\n\t<th>Winery</th>" . "\n\t<th>Description</th>\n</tr>"; // Fetch each of the query rows while ($row = @ mysql_fetch_array($result)) { // Print one row of results echo "\n<tr>" . "\n\t<td>" . $row["wine_id"] . "</td>" . "\n\t<td>" . $row["wine_name"] . "</td>" . "\n\t<td>" . $row["type"] . "</td>" . "\n\t<td>" . $row["year"] . "</td>" . "\n\t<td>" . $row["winery_name"] . "</td>" . "\n\t<td>" . $row["description"] . "</td>" . "\n</tr>"; ...Read now
Unlock full access