Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8236701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:13:09+00:00 2026-06-07T19:13:09+00:00

I´m reading Kevin Yank´s book PHP and MySQL Novice to Ninja 5th edition ,

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:13:10+00:00Added an answer on June 7, 2026 at 7:13 pm

    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.

    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 PHP Code:

    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.

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading sitepoint's book PHP & MySQL Novice to Ninja and I'm wondering why
Reading the Scala by Example book and there is this example when Martin explains
Reading this question I found this as (note the quotation marks) code to solve
I was reading about how to implement a DPDA and found this code in
Reading the Jon Skeet book, I have found (some time now) the use of
Reading the Oxite source code, I have found that validators save bad property name
reading the SCJP book, I've found something like this in the chapter 1 self-test
Reading the docs, I'd expect $(#wrap2).remove(.error) to remove all .error elements from #wrap2 .
Reading the MCIMX50 Application Processor Reference Manuals on page 1368 (Section 33.3) there is
Reading over some example Objective C code just now. @property (nonatomic, strong) IBOutlet UILabel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.