热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将mysql代码转换为PDO不会产生任何输出-ConvertingmysqlcodetoPDOgivesnooutput

IamtryingtoconvertthecodegivenheretoPDOusingOOPapproach.ThisiswhatIvegotsofar:

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

我试图使用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 error.

Firebug抛出未定义的变量:注释错误。

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?

我哪里出错了?

3 个解决方案

#1


2  

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

当我打算使用$ x <$ numrows时,你正在使用$ x <$ row

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.

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

#2


0  

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

你的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"]);       
}

#3


0  

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++) {

推荐阅读
author-avatar
楼外蔷薇花开
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有