Hello everyone - I have picked dup a book on php and SQL and have been quaking through it. I have come across a problem with a foreach statement that I can not resolve.
Any help and advice is greatly appreciated!
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=ijdb; charset=utf8',
'ijdbuser', 'mypassword');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT `joketext` FROM `joke`';
$result = $pdo->query($sql);
while ($row = $result->fetch()) {
$jokes[] = $row['joketext'];
}
}
catch (PDOException $e) {
$output = 'Unable to connect to the database server: ' .
$e->getMessage() . ' in ' .
$e->getFile() . ':' . $e->getLine();
}
include __DIR__ . '/../templates/jokes.html.php';
and the other file
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>List of jokes</title>
</head>
<body>
<?php if (isset($error)): ?>
<p>
<?php echo $error; ?>
</p>
<?php else: ?>
<?php foreach($jokes as $joke ): ?>
<blockquote>
<p>
<?php echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8') ?>
</p>
</blockquote>
<?php endforeach; ?>
<?php endif; ?>
</body>
</html>