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 7693241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:02:31+00:00 2026-05-31T21:02:31+00:00

Just trying to get some rows out of a database and loop through but

  • 0

Just trying to get some rows out of a database and loop through but I am get the non-object error.

    $db = classes_pdoDB::getConnection();
    $query = "SELECT *
              FROM lesson
              WHERE userID=:userID";
    $stmt = $db->prepare($query);
    $stmt->execute(array(':userID' => $userID));
    $lessons = $stmt->fetchAll();

    foreach ($lessons as $lesson){

        print_r ($lesson);

        $page->addToBody ("<div class=\"editLessonEntry\">
            <p><a href=\"editLesson.php?lessonid=" . $lesson->lessonID . "\" >" . $lesson->lessonTitle . "</a></p>
            <p>" . $lesson->lessonSummary . " </p>
        </div>
        <hr />");

    }

Not really certain what the error is because the print_r is working fine and showing me all the objects which I am trying to include. I’m sure like all of my questions I am just overlooking something incredibly simple. But I really cannot see it. I have other similar functions through the site which work fine and I can’t see the differences really.

  • 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-05-31T21:02:32+00:00Added an answer on May 31, 2026 at 9:02 pm

    fetchAll() returns an array unless you have specified the class to return with PDO::FETCH_CLASS. So in this case, you should be accessing $lesson by array elements rather than object properties:

    $lessons = $stmt->fetchAll();
    
    foreach ($lessons as $lesson){
    
        print_r ($lesson);
    
        $page->addToBody ("<div class=\"editLessonEntry\">
            <p><a href=\"editLesson.php?lessonid=" . $lesson['lessonID'] . "\" >" . $lesson['lessonTitle'] . "</a></p>
            <p>" . $lesson['lessonSummary'] . " </p>
        </div>
        <hr />");
     }
    

    If you must get these as an object and have a Lesson class, you can do it this way:

    $lessons = $stmt->fetchAll(PDO::FETCH_CLASS, "Lesson");
    

    This assumes the Lesson class takes no parameters to its constructor. This is detailed in the fetchAll() documentation.

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

Sidebar

Related Questions

Im trying to get this working but for some reason it's just not right.
So I just recently bought some web hosting and I'm trying to get my
Just trying to get my head round Spring and figuring out how I wire
Just trying out ubuntu server on my pc and have been testing some commands
I'm trying to get some simple data into a JsonStore, but it doesn't seem
Just trying to get my head around Generics by reading this enlightening article by
Just trying to get my irb sessions to actually list the current line of
Just trying to get up to speed with the SDK... So, I've created my
Just trying to get diff to work better for certain kinds of documents. With
Just trying to get my head around what can happen when things go wrong

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.