\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"; } $first = filter_input(INPUT_GET, "firstName"); $last = filter_input(INPUT_GET, "lastName"); try { if (empty($first) || empty($last)) { throw new Exception("Missing first or last name."); } print "

Students of $first $last

\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 student.first, student.last, subject ". "FROM student, teacher, class, student_class ". "WHERE teacher.last = :last ". "AND teacher.first = :first ". "AND teacher_id = teacher.id ". "AND code = class_code ". "AND student.id = student_id ". "ORDER BY subject, student.last"; $ps = $con->prepare($query); // Fetch the matching rows. $ps->execute(array(':first' => $first, ':last' => $last)); $data = $ps->fetchAll(PDO::FETCH_ASSOC); // $data is an array. if (count($data) > 0) { constructTable($data); } else { print "

(No students.)

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