Customize the file by adding five more records and run the required query. Make
ID: 3547735 • Letter: C
Question
Customize the file by adding five more records and run the required query. Make sure you create the new table under your database that you created for your earlier application. There should be at-least 10 rows with data (you can create more if you would like).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Book Club Members Under 25</title>
<link rel="stylesheet" type="text/css" href="common.css" />
</head>
<body>
<h1>Book Club Members Under 25</h1>
<?php
$dsn = "mysql:dbname=mydatabase";
$username = "root";
$password = "mypass";
try {
$conn = new PDO( $dsn, $username, $password );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo "Connection failed: " . $e->getMessage();
}
$sql = "SELECT * FROM members WHERE age < 25";
echo "<table><tr><th>First Name</th><th>Last Name</th><th>Age</th><th>Joined</th></tr>";
try {
$rows = $conn->query( $sql );
foreach ( $rows as $row ) {
echo "<tr><td>" . $row["firstName"] . "</td><td>" . $row["lastName"] . "</td><td>" . $row["age"] .
"</td><td>" . $row["joinDate"] . "</td></tr>";
}
} catch ( PDOException $e ) {
echo "Query failed: " . $e->getMessage();
}
echo "</table>";
$conn = null;
?>
</body>
</html>
Explanation / Answer
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Book Club Members Under 25</title>
<link rel="stylesheet" type="text/css" href="common.css" />
</head>
<body>
<h1>Book Club Members Under 25</h1>
<?php
$dsn = "mysql:dbname=mydatabase";
$username = "root";
$password = "mypass";
try {
$conn = new PDO( $dsn, $username, $password );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo "Connection failed: " . $e->getMessage();
}
$insert1="insert into members values ('john','paul',35,28-01-1988)";
$insert2="insert into members values ('harry','kong',32,18-03-1984)";
$insert3="insert into members values ('jim','parry',26,24-05-1982)";
$insert4="insert into members values ('kent','leon',29,07-10-1990)";
$insert5="insert into members values ('william','bing',29,12-05-1987)";
$sql = "SELECT * FROM members WHERE age < 25";
echo "<table><tr><th>First Name</th><th>Last Name</th><th>Age</th><th>Joined</th></tr>";
try {
$rows = $conn->query( $sql );
foreach ( $rows as $row ) {
echo "<tr><td>" . $row["firstName"] . "</td><td>" . $row["lastName"] . "</td><td>" . $row["age"] .
"</td><td>" . $row["joinDate"] . "</td></tr>";
}
} catch ( PDOException $e ) {
echo "Query failed: " . $e->getMessage();
}
echo "</table>";
$conn = null;
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.