Example in Kevin Yank´s book won´t work (involves INNER JOIN and foreach()) page 151

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?

Thanks again for your help!!! Rosamunda

It is because of your php settings to show Notices and Warnings. The notice/warning is valid because you were attempting to use the $jokes variable before you declared/assigned a value to it. You can solve this by putting $jokes = array(); before

foreach ($result as $row) 
{ 
    $jokes[] = array( 
    'id' => $row['id'],  
    'text' => $row['joketext'],  
    'date' => $row['jokedate'], 
    'name' => $row['name'], 
    'email' => $row['email'] 
    ); 
} 

That will at least declare the $jokes variable for in the event that there are zero results.

Oh I see!! THANKS!!!

In order to handle the situation where you have no jokes in your database, you would need to set the $jokes variable to contain an empty array before filling it with your database records (which doesn’t happen if there are no database records!). You can do this by adding a statement before the foreach in your index.php:

$jokes = array();

Hope that helps!