\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"; $ps = $con->prepare($query); // Fetch the matching row. $ps->execute(array(':id' => $id)); $data = $ps->fetchAll(PDO::FETCH_ASSOC); // $data is an array. if (count($data) > 0) { constructTable($data); } else { print "

(No match.)

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