Page 117 - PHP Novice to Ninja

I used this code:

while($row = $result->fetch())
{
    $jokes[] = $row['joketext'];
}
include 'jokes.html.php';

and had this response:
Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\yankproject\listjokes\index.php on line 27

Has anyone encountered this, I am stuck here, please help!!

You probably have a typo. Look at the line where you executed the query. What name is it assigned to?

If you can provide the whole file, that would help us find the problem. Right now, we don’t have enough information to give you the correct advice.

try
{
    $pdo = new PDO('mysql:host=localhost;dbname=ijdb','user','password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $pdo->exec('SET NAMES "utf8"');
} 
catch (PDOException $e) 
{
    $error = 'Error Unable to connect at this time please try later';
    include 'error.html.php';
    exit();
}

try
{
    $sql = 'SELECT  joketext FROM joke';
    $result = $pdo->quote($sql);
} 
catch (PDOException $e) 
{
    $error = 'Error fetching jokes: '.$e->getMessage();
    include 'error.html.php';
    exit();
}

while($row = $result->fetch())
{
    $jokes[] = $row['joketext'];
}
include 'jokes.html.php';

Because $result is defined in the if statement, it’s local to that statement. Take your whole while statement and move it inside the catch after the $pdo->quote statement.

try
{
    $pdo = new PDO('mysql:host=localhost;dbname=ijdb','user','password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $pdo->exec('SET NAMES "utf8"');
} 
catch (PDOException $e) 
{
    $error = 'Error Unable to connect at this time please try later';
    include 'error.html.php';
    exit();
}

try
{
    $sql = 'SELECT  joketext FROM joke';
    $result = $pdo->quote($sql);
    while($row = $result->fetch())
{
    $jokes[] = $row['joketext'];
}
}
catch (PDOException $e) 
{
    $error = 'Error fetching jokes: '.$e->getMessage();
    include 'error.html.php';
    exit();
}


include 'jokes.html.php';

First of all Thank You!!

Here`s the response I got after the change:
Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\yankproject\listjokes\index.php on line 19

$pdo->quote is probably supposed to be $pdo->query

Thank You Jeff! Bless that set of eyes!!

Something weird still goes on:

my NetBeans IDE is not accepting “endforeach” as shown in page 117 it instead gives me an error:
Parse error: syntax error, unexpected ‘endforeach’ (T_ENDFOREACH) in C:\xampp\htdocs\yankproject\listjokes\jokes.html.php on line 12. And, if I eliminate the endforeach command I get only the 5th joke and jokes 1 to 4 are missing. Any ideas?

Please post your latest code and the content of your jokes.html.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>List of Jokes</title>
    </head>
    <body>
        <p>Here are all the jokes in the database</p>
        <?php foreach ($jokes as $joke); ?>
        <blockquote>
            <p><?php echo htmlspecialchars($joke, ENT_QUOTES, 'utf-8'); ?></p>
        </blockquote>
        <?php endforeach; ?>
    </body>
</html>

This code is almost like a copy paste of the code given in the book, I wondered if this is to do with a version change. I appreciate your help!!

I actually caught the typo. I used a ; instead of : at the end of the foreach statement. But I thank you guys . This is an awesome forum, I am grateful.

Thanks again!!

1 Like

Do you understand why the “;” stopped it at 1 joke? In case not, the ; is a terminating character, so it terminated your foreach statement which would typically loop over each joke in $jokes after processing the first one. That is why you only got one joke when using “;” instead of “:”.

Thank you for breaking that down for me! You`ve been a huge help!!

I had left off learning for a little while and trying the same code again and I am past the colon semicolon issue, but still getting this error: kindly comment

Notice: Undefined variable: jokes in C:\xampp\htdocs\jokes\home\jokes.html.php on line 8

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\jokes\home\jokes.html.php on line 8

You’re getting that error because the foreach is expecting an array to work with, it will have received a string instead as you probably had an empty result set returned by a query. Before doing any SELECT query, set up the variable that you’re going to use for the result set as an empty array eg, say you’re going to get a list of users who matches criteria x, y and z, and was going to place the result set into $da_users Before running the query you would do:

$da_users = array();

That will set up the $da_users variable as an empty array. If there was a result set returned then you’d load it into $da_users as normal, if there was no result set returned then the foreach will see what it’s expecting to be given to work with, an array, albeit en empty array.