showQuery.php

Source of showQuery.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>showQuery.php</title>
<link rel = "stylesheet"
      type = "text/css"
      href = "showQuery.css" />
      
</head>

<body>
<?php



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

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

  function readQuery(){
    global $query;
    //reads in the query from a file
    $queryName = $_REQUEST["queryName"];
    print "<h1>$queryName</h1>";
    $query = file_get_contents($queryName . ".sql");
  } // end readQuery

  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", "user", "password");
    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", "password");
    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>