作者:楼外蔷薇花开 | 来源:互联网 | 2023-02-13 10:36
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 个解决方案