将 mysql 代码转换为 PDO 没有输出
我正在尝试转换 此处给出的代码 使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用
$x < $row
当我认为你打算使用$x
$numrows
整个循环可以这样更好地编写:
如果使用
$comments[]
语法,则不需要$x
计数器,因为这样会将带有数字键的每个新行附加到数组中。You are using
$x < $row
when I think you intend to use$x < $numrows
This whole loop could be better written this way:
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.你的 for 循环是错误的。您需要获取行数,然后在每次循环迭代时调用
$query->fetch()
:Your for loop is wrong. You need to get the number of rows, then call
$query->fetch()
on every loop iteration:以下行有语法错误(逗号而不是分号):
应该是:
The following line has a syntax error (comma instead of semicolon):
It should be: