将 mysql 代码转换为 PDO 没有输出

发布于 2024-11-08 22:26:05 字数 1891 浏览 0 评论 0原文

我正在尝试转换 此处给出的代码 使用 OOP 方法实现 PDO。这是我到目前为止所得到的:

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 错误。

这是原始代码:

  $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;

我哪里出错了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪落纷纷 2024-11-15 22:26:05

您正在使用 $x < $row 当我认为你打算使用 $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++)

整个循环可以这样更好地编写:

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

如果使用 $comments[] 语法,则不需要 $x 计数器,因为这样会将带有数字键的每个新行附加到数组中。

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.

巾帼英雄 2024-11-15 22:26:05

你的 for 循环是错误的。您需要获取行数,然后在每次循环迭代时调用 $query->fetch()

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

Your for loop is wrong. You need to get the number of rows, then call $query->fetch() on every loop iteration:

$numrows = //...
for ($x = 0; $x < $numrows; $x++) {
  $row = $query->fetch(PDO::FETCH_ASSOC);
  $comments[$x] = array("name" => $row["name"], "comment" => $row["comment"], "date" => $row["date"]);       
}
杀手六號 2024-11-15 22:26:05

以下行有语法错误(逗号而不是分号):

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

应该是:

for ($x = 0; $row = $query->fetch(PDO::FETCH_ASSOC); $x < $row; $x++) {

The following line has a syntax error (comma instead of semicolon):

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

It should be:

for ($x = 0; $row = $query->fetch(PDO::FETCH_ASSOC); $x < $row; $x++) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文