I´m reading Kevin Yank´s book “PHP and MySQL Novice to Ninja 5th edition“, and found an error there in the code, and would like someone to help me out with it, maybe is a silly typo…?
I´m trying to follow the author´s example of creating and accessing a database of jokes. I´m learning how to join two databases to show with php a list of all the jokes.
I have two databases joke and author.
I´ve got this:
try{
$sql = 'SELECT joke.id, joketext, jokedate, name, email
FROM joke INNER JOIN author
ON authorid = author.id';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error: ' . $e->getMessage();
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$jokes[] = array(
'id' => $row['id'],
'text' => $row['joketext'],
'date' => $row['jokedate'],
'name' => $row['name'],
'email' => $row['email']
);
}
include 'jokes.html.php';
Now, all was working ok, until I´ve replaced the simple code to select the database information from just one table, to the INNER JOIN code.
This is the book´s code, wich I´ve followed.
In the jokes.html.php file, I´ve got this (wich I think is what´s giving me the error):
foreach($jokes as $joke):
<form action="?deletejoke" method="post">
<?php
echo 'id. ';
echo htmlspecialchars($joke['id'], ENT_QUOTES, 'UTF-8');
echo htmlspecialchars($joke['date'], ENT_QUOTES, 'UTF-8');
echo htmlspecialchars($joke['text'], ENT_QUOTES, 'UTF-8');
echo htmlspecialchars($joke['name'], ENT_QUOTES, 'UTF-8');
echo htmlspecialchars($joke['email'], ENT_QUOTES, 'UTF-8');
?>
<input type="hidden" name="id" value="<?php echo $joke['id'];?>">
<input type="submit" value="Borrar">
?>
<br></form>
<?php endforeach; ?>
Now, the error that throws me is:
Notice: Undefined variable: jokes in C:\xampp\htdocs\workspace1\jokes.html.php on line 10
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\workspace1\jokes.html.php on line 10
Line 10 of jokes.html.php is:
foreach($jokes as $joke):
I´m trying to get more information about foreach() but I can´t spot the error…
If anyone could help me out a bit (or maybe a clue!) I would be very grateful.
Thanks!!!
Rosamunda
UPDATE:
As the result of the query (trying it directely from phpmyadmin) was zero, so there were no database results for that query. I´ve decided to manually add one result doing this:
INSERT INTO joke SET
joketext = 'this is a new joke....',
jokedate = '2012-01-01',
authorid = 1;
Now, the errors have dissapear, and that single results does show.
What I don´t understand is:
Why didn´t just no result showed up, instead of those errors?
How do you manage these situations? I mean, it can happen that a query just have no results at all, is it common to result in those errors?
One of your helpful comments says that $result is empty… so why when the query isn´t zero those errors won´t show up?
Thanks again for your help!!! Rosamunda
I´ve found the answer in a SitePoint forum (the Book´s forum), and I thought that it would be nice to post the question here, just in case anyone wonders, or just in case anyone out there happens to have the same problem.
So, I think the conclusion (please correct me if I´m wrong here!) is that you should always declare any variable that you pretend to use, just in case it happens to have no results. Because if it is empty it will show a nasty error message that will freak you out.
And to declare a variable you use
$variablename = array().