Problem with foreach from book study

I’m studying from the book of which the author is Kevin Yank,
I’m in chapter 5, the book advised me to turn to this forum.
I’m doing the exercise using two separate databases so as to practice both with the example from the book and be with my idea,
For now I posted the picture with the error that I’m from, someone can tell me yours? :shifty:


Sorry but images need to get approved and we do check from time to time but if you think that we’re taking too long, do get in touch with a member of the staff and send him a pm asking him to do so.

I can’t really see what’s wrong. Your error clearly indicates that there’s something missing in your foreach() and probably is a variable (jokes) that has no value.

Now, you say that this is a book written by Kevin Yank. He wrote several books but because you’re talking about “jokes”, I think that this book in particular is “Build your own database driven website with PHP and MySQL”

Chapter 5 is dedicated to the relationships between tables. Could it be the case that the table jokes was completely empty?

Please post the php code, so we can take a look

If I remember the code correctly $jokes is the array that the jokes returned from the database is loaded into. A foreach loop expects to be given an array to iterate (loop) through, but when there are no jokes returned by the query, all it sees is an empty variable instead.

Just before you grab the result set, add this:


That will set up the $jokes variable as an empty array, ready to have any jokes returned by the database added to it. I’m surprised that the error has not been corrected by now as it’s existed in the last two editions of the book at least. @HAWK; Could you please raise it with whoever at hq deals with errors/errata in SitePoint books? ty

thanks for reply

I found that did not work because I had the authors table in the database EMPTY: D
… so I solved the problem. But it is also true that the book does not warn: D


Well readin that “Error” the first one is telling you that $jokes is not defined and thats your problem to start with. You should check you code to see that you have infact got an array called $jokes defined then take it from there.

foreach ($result as $row)
  $jokes[] = array(
    'id' => $row['id'],
    'news' => $row['testonotizia'],
    'name' => $row['name'],
    'email' => $row['email']

include 'jokes.html.php';

Aaaaaaah! This is so much better! Thank you @phpita;

The thing is that I cannot always have access to the text directly so seeing your code helps.

Still, since you’ve solved the problem (well done to you :D)… Am I right to assume that you don’t need further help? (at least, for the moment?)

No, thanks, i continue study