hero.php

Source of hero.php

<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang = "EN" xml:lang = "EN" dir = "ltr">
<head>
<meta http-equiv="content-type" content="text/xml; charset=iso-8859-1" />
<title>hero.php</title>
<style type = "text/css">
  h1, h2, h3 {
    text-align: center;
    color: #CCCCFF;
  }
  body {
    background-color: #3333AA;
  }
  label, textarea, button {
    display: block;
    margin-left: auto;
    margin-right: auto;
  }
  table {
    background-color: #CCCCFF;
    margin-left: auto;
    margin-right: auto;
    border: 1px black solid;
  }
  td, th {
    border: 1px black solid;
  }
  pre {
    background-color: white;
    border: 3px black double;
    width: 60%;
    margin-left: auto;
    margin-right: auto;
  }

</style>
</head>

<body>
  <h1>hero.php</h1>

<?php

  $query = <<<HERE
SELECT * FROM hero;
HERE;


showQuery();
showResult();
//showResultTabbed();


  function showQuery(){
    global $query;

    print "<h2>Query</h2> \n";
    print "<pre> \n";
    print $query;
    print "</pre> \n";
  } // end showQuery

  function showResult(){
    global $query;
    print "<h2>Result</h2> \n";
    
    $con = mysql_connect("localhost", "xfd", "xfdaio");
    mysql_select_db("xfd");
    $result = mysql_query($query);
    if ($result == -1){
      print mysql_error();
    } else {
      print "<table border = '1'> \n";
      //print field names    
      print "  <tr> \n";
      $row = mysql_fetch_assoc($result);
      foreach ($row as $field => $value){
        print "    <th>$field</th> \n";
      } // end foreach
      print "  </tr> \n";
    
      //start over, this time getting values instead
      $result = mysql_query($query);
      while ($row = mysql_fetch_assoc($result)){
        print "  <tr> \n";
        foreach ($row as $field => $value){
          print "    <td>$value</td> \n";
        } // end foreach
        print "  </tr> \n\n";
      } // end while   
    
      print "</table> \n"; 
    } // end if
  } // end showResult

  function showResultTabbed(){
    //creates tabbed output useful for inclusion in book
    //not used in online version
    global $query;
     //print "<p>query: $query.</p>";
    
    $con = mysql_connect("localhost", "xfd", "xfdaio");
    mysql_select_db("xfd");
    $result = mysql_query($query);
    print "<pre> \n";
    //print field names    
    $row = mysql_fetch_assoc($result);
    foreach ($row as $field => $value){
      print "$field \t";
    } // end if
    print "\n";
    
    //start over, this time getting values instead
    $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)){
      foreach ($row as $field => $value){
        print "$value \t";
      } // end foreach
      print "\n";
    } // end while   
    
    print "</pre> \n"; 
  } // end showResultTabbed

?>

</body>
</html>