<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>Query Results</title> </head> <body> <h1>Query Results</h1> <?php class Person { private $id; private $first; private $last; private $gender; private $salary; public function getId() { return $this->id; } public function getFirst() { return $this->first; } public function getLast() { return $this->last; } public function getGender() { return $this->gender; } public function getSalary() { return $this->salary; } } $first = filter_input(INPUT_GET, "firstName"); $last = filter_input(INPUT_GET, "lastName"); try { // Connect to the database. $con = new PDO("mysql:host=localhost;dbname=supercoders", "supercoders", "sesame"); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $query = "SELECT * FROM people"; // Fetch the matching database table rows. $data = $con->query($query); $data->setFetchMode(PDO::FETCH_CLASS, "Person"); // We're going to construct an HTML table. print " <table border='1'>\n"; // Fetch the database field names. $result = $con->query($query); $row = $result->fetch(PDO::FETCH_ASSOC); // Construct the header row of the HTML table. print " <tr>\n"; foreach ($row as $field => $value) { print " <th>$field</th>\n"; } print " </tr>\n"; // Constrain the query if we got first and last names. if ((strlen($first) > 0) && (strlen($last) > 0)) { $query = "SELECT * FROM people ". "WHERE first = :first ". "AND last = :last"; $ps = $con->prepare($query); $ps->bindParam(':first', $first); $ps->bindParam(':last', $last); } else { $ps = $con->prepare($query); } // Fetch the matching database table rows. $ps->execute(); $ps->setFetchMode(PDO::FETCH_CLASS, "Person"); // Construct the HTML table row by row. while ($person = $ps->fetch()) { print " <tr>\n"; print " <td>" . $person->getId() . "</td>\n"; print " <td>" . $person->getFirst() . "</td>\n"; print " <td>" . $person->getLast() . "</td>\n"; print " <td>" . $person->getGender() . "</td>\n"; print " <td>" . $person->getSalary() . "</td>\n"; print " </tr>\n"; } print " </table>\n"; } catch(PDOException $ex) { echo 'ERROR: '.$ex->getMessage(); } ?> </body> </html>