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

  • Home
  • SEARCH
  • 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 5956959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:19:56+00:00 2026-05-22T18:19:56+00:00

I am trying to convert the code given here to PDO using OOP approach.

  • 0

I am trying to convert the code given here to PDO using OOP approach. This is what I’ve got so far:

comments.PHP:

public function loadComments() {
              $sql = "SELECT * FROM `comments`
                      WHERE
                      `comments`.`ImageID`  = :imageid ;";

            try
            {
                $imageid = $_REQUEST['imageid'];



                $query = $this->_db->prepare($sql);
                $params = array(':imageid' => $imageid);
                $query->execute($params); 

                for ($x = 0, $row = $query->fetch(PDO::FETCH_ASSOC); $x < $row; $x++) {
                 $comments[$x] = array("name" => $row["name"], "comment" => $row["comment"], "date" => $row["date"]);       
                    }

                $response = $_GET["jsoncallback"] . "(" . json_encode($comments) . ")";
                echo $response;
                return TRUE;

            }
            catch(Exception $ex)
            {
                return FALSE;
            }
    }

Firebug throws the undefined variable: comments error.

This is the original code:

  $query = mysql_query("SELECT
                          * FROM `comments`
                          WHERE
                          `comments`.`ImageID`  = '$imageid' ;");

        //loop through and return results
      for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
            $row = mysql_fetch_assoc($query);

            $comments[$x] = array("name" => $row["name"], "comment" => $row["comment"], "date" => $row["date"]);            
        }

        //echo JSON to page
        $response = $_GET["jsoncallback"] . "(" . json_encode($comments) . ")";
        echo $response;

Where have I gone wrong?

  • 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-22T18:19:57+00:00Added an answer on May 22, 2026 at 6:19 pm

    You are using $x < $row when I think you intend to use $x < $numrows

    for ($x = 0, $row = $query->fetch(PDO::FETCH_ASSOC); $x < $row; $x++)
                                                              ^^^^^
    $numrows = $query->rowCount();
    for ($x = 0, $row = $query->fetch(PDO::FETCH_ASSOC); $x < $numrows; $x++)
    

    This whole loop could be better written this way:

    while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
      $comments[] = array("name" => $row["name"], "comment" => $row["comment"], "date" => $row["date"]);       
    }
    

    There’s no need for the $x counter if you use the $comments[] syntax, since that will append each new row with a numeric key onto the array.

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

Sidebar

Related Questions

I am trying to convert the code snippet given in this http://www.movable-type.co.uk/scripts/latlong.html into java.
I am trying to convert this test code to C# and having a problem
Given this 3D bar graph sample code , how would you convert the numerical
I'm trying to convert a function from Javascript to CoffeeScript. This is the code:
I'm trying to convert some code that worked great in VB, but I can't
I'm trying to convert a character code to a character with chr(), but VBScript
I'm trying to convert old QuickTime framework code to the 64-bit Cocoa-based QTKit on
HI, I am trying to convert the following vba code to c# and i
I've been trying to convert SVG images to PNG using C#, without having to
Here's the deal: I'm trying, as a learning experience, to convert a C program

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.