showQuery.php

Source of showQuery.php

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>showQuery.php</title>
  <link rel = "stylesheet"
        type = "text/css"
        href = "../../css/custom-theme/jquery-ui-1.7.2.custom.css" />
  <link rel = "stylesheet"
        type = "text/css"
        href = "../../css/custom-theme/jad.css" />
  <style type = "text/css">
  h1 {
    font-family: sans-serif;
    width: 50%;
  }
    
  h1, h2 {
    text-align: center;
    background-image: none;
    height: auto;
    margin-left: auto;
    margin-right: auto;
    box-shadow: 5px 5px 5px gray;
  }
  
  table {
    margin-left: auto;
    margin-right: auto;
  }
  
  pre {
    width: 80%;
    margin-left: auto;
    margin-right: auto;
    box-shadow: 5px 5px 5px gray;
  }
  
  table {
    box-shadow: 5px 5px 5px gray;
  }
  
  </style>

</head>

<body>
<?php



  $query = <<<HERE
SELECT * FROM hero;
HERE;
try {
  readQuery();
  showQuery();
  showResult();
} catch(PDOException $e) {
  echo 'ERROR: ' . $e->getMessage();
} // end try

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

  function showQuery(){
    global $query;

    print "<h2 class = \"ui-widget-header ui-corner-all\">Query</h2> \n";
    print "<pre> \n";
    print $query;
    print "</pre> \n";
  } // end showQuery

  function showResult(){
    global $query;
    print "<h2 class = \"ui-widget-header ui-corner-all\">Result</h2> \n";
    
    $con= new PDO('mysql:host=localhost;dbname=dbName', "user", "pwd");
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        //first pass just gets the column names
      print "<table border = \"1\" class = \"ui-widget ui-widget-content ui-corner-all\"> \n";
        print "  <tr> \n";

        $result = $con->query($query);
        $row = $result->fetch(PDO::FETCH_ASSOC);

        foreach ($row as $field => $value){
          print "  <th>$field</th> \n";
        } // end foreach
        print "  </tr> \n";
    
      //start over, this time getting values instead

      $result = $con->query($query);
      $result->setFetchMode(PDO::FETCH_ASSOC);
      foreach($result as $row){
        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 showResult

?>

</body>
</html>