Querying with User Input
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>"; ...
Get Web Database Applications with PHP, and MySQL now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.