[英]Cannot display results from query with PDO
I am trying to get the results from a query which should get multiple results and display them all on the page. 我试图从查询中获取结果,该查询应获取多个结果并将其全部显示在页面上。 However it is not displaying any of the content.
但是,它不显示任何内容。 My guess is a mistake in my syntax for me loop.
我的猜测是我的循环语法错误。 But I am unsure.
但是我不确定。
//query to find comments about this map
$query = "
SELECT
user_id,
comment
FROM map_comments
WHERE
map_id = :mapID
";
//query parameters
$query_params = array(
':mapID' => $_SESSION['mapID']
);
try
{
//execute query
$statement = $db->prepare($query);
$result = $statement->execute($query_params);
//get all results
$comments = $result->fetchAll;
if($result === FALSE)
{
die(mysql_error()); // TODO: better error handling
}
}
catch(PDOException $e)
{
die("failed to find comments");
}
foreach($comments as &$comment)
{
echo $comment;
}
You need parentheses after a function to call it. 您需要在函数后加上括号才能调用它。
$comments = $result->fetchAll;
should be: 应该:
$comments = $statement->fetchAll();
Also, the check for if ($result == FALSE)
should be before this line. 另外,检查
if ($result == FALSE)
是否应在此行之前 。 And you can't use mysql_error()
if you're using PDO, you should use $statement->errorInfo()
. 如果使用PDO,则不能使用
mysql_error()
,而应使用$statement->errorInfo()
。 Or you should enable PDO::ERRMODE_EXCEPTION
on the connection, and the catch
block will be invoked. 或者,您应该在连接上启用
PDO::ERRMODE_EXCEPTION
,然后catch
块将被调用。 You should then use $db->errorInfo()
in the error message that it prints. 然后,您应该在打印的错误消息中使用
$db->errorInfo()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.