\n"; // Construct the HTML table row by row. $doHeader = true; foreach ($data as $row) { // The header row before the first data row. if ($doHeader) { print " \n"; foreach ($row as $name => $value) { print " $name\n"; } print " \n"; $doHeader = false; } // Data row. print " \n"; foreach ($row as $name => $value) { print " $value\n"; } print " \n"; } print " \n"; } $id = filter_input(INPUT_GET, "id"); try { if (empty($id)) { throw new Exception("Missing id."); } print "

Teacher with id $id

\n"; // Connect to the database. $con = new PDO("mysql:host=localhost;dbname=school", "root", "sesame"); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $query = "SELECT * FROM teacher WHERE id = $id"; // Fetch the matching row. $data = $con->query($query); $data->setFetchMode(PDO::FETCH_ASSOC); // $data is a result set. if ($data->rowCount() > 0) { constructTable($data); } else { print "

(No match.)

\n"; } } catch(PDOException $ex) { echo 'ERROR: '.$ex->getMessage(); } catch(Exception $ex) { echo 'ERROR: '.$ex->getMessage(); } ?>