Query Results
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; }
}
function createTableRow(Person $p)
{
print " \n";
print " " . $p->getId() . " | \n";
print " " . $p->getFirst() . " | \n";
print " " . $p->getLast() . " | \n";
print " " . $p->getGender() . " | \n";
print " " . $p->getSalary() . " | \n";
print "
\n";
}
$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 " \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 " \n";
foreach ($row as $field => $value) {
print " $field | \n";
}
print "
\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 " \n";
createTableRow($person);
print "
\n";
}
print "
\n";
}
catch(PDOException $ex) {
echo 'ERROR: '.$ex->getMessage();
}
?>