我試圖使用OOP方法將給定的代碼here轉換爲PDO。這是我到目前爲止有:將mysql代碼轉換爲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;
}
}
螢火蟲拋出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;
我在哪裏出了錯?
謝謝。那樣做了! – input 2011-05-20 20:04:16