Workshop07/PHP MySQL Solution

<!DOCTYPE HTML PUBLIC
                 "-//W3C//DTD HTML 4.01 Transitional//EN"
                 "http://www.w3.org/TR/html401/loose.dtd">
<?php
/* $Id: jokestable.php  */
/**
 *
 * This program  displays a table of light bulb jokes, generated from  a MySQL database.
 *
 * This file gives a solution to the following exercise:
 * Create a MySQL database that contains one table. The jokes table stores the light 
 * bulb jokes. For each joke, the table stores the joke's subject (e.g., professors, 
 * computer scientists) and the joke's punchline. Populate the table with sample data.
 *
 * Subject  Punchline
 *
 * @author     R. Morelli <ralph.morelli@trincoll.edu>
 * @version    1.0
 * @package    default
 * @license    http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL) 
 *
 */
?>

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>Light Bulb Jokes</title>
</head>
<body>
<center>
<h2>Light Bulb Jokes</h2>
<h3>(Generated from Workshop Database by jokestable.php)</h3>
<table border=1>
<tr><th>Subject</th><th>Punchline</th></tr>
<?php

/**
 * Display's an error message after a faulty DB operation
 *
 * @access public
 * @return void
 */
   function showerror()
   {
      die("Error " . mysql_errno() . " : " . mysql_error());
   }

// Here starts the main program.

   // (1) Open the database connection
   if (!($connection = @ mysql_connect("localhost", "root", "test1234")))
      die("Could not connect");

   // (2) Select the workshop database
   if (!(@ mysql_select_db("workshop", $connection)))
      showerror();

   // (3) Run a query through the connection getting all rows of the  jokes table 
   if (!($result = @ mysql_query ("SELECT * FROM jokes", $connection)))
      showerror();

   // (4) While there are still rows in the result set, fetch the current row into the array $row
   while ($row = @ mysql_fetch_array($result, MYSQL_NUM))
   {
     // (5) Get the data from the fields of the jokes table, the first field is $row[0], etc.
     $subject =  $row[0];
     $punchline = $row[1];

     // (6) Display one row of the table
     print "<tr><td>{$subject}</td><td>{$punchline}</td></tr>";
   }
?>
</table>
<center>
</body>
</html>