Php PDO语句不显示获取的结果
好吧,我从头开始创建了一个博客,错误地开始用mysql_uu语句编写它,所以我回去用PDO语句重写了它。我现在在显示数据库中的博客文章时遇到问题Php PDO语句不显示获取的结果,php,pdo,fetchall,Php,Pdo,Fetchall,好吧,我从头开始创建了一个博客,错误地开始用mysql_uu语句编写它,所以我回去用PDO语句重写了它。我现在在显示数据库中的博客文章时遇到问题 <?php include 'includes.php'; echo '<h3>-----------------------------------------------------</h3>'; $blogIndex = 'SELECT * FROM blog_post ORDER BY d
<?php
include 'includes.php';
echo '<h3>-----------------------------------------------------</h3>';
$blogIndex = 'SELECT * FROM blog_post ORDER BY date_posted DESC';
$blogIndexstmt = $DBH->prepare($blogIndex);
$blogIndexRows = $blogIndexstmt->fetchAll();
if(!$blogIndexRows) {
echo 'No Post Yet.';
}
else {
while($blogIndexRows->nextRowset()) {
echo '<h2>' . $row['title'] . '</h2>';
$datePosted = $row['date_posted'];
echo '<p>' . gmdate('F j, Y, g:i a', $datePosted) . '</p>';
$body = substr($row['post'], 0, 300);
echo nl2br($body) . '...<br/>';
echo '<a href="post_view.php?id=' . $row['id']. '">Read More</a> | ';
echo '<a href="post_view.php?id=' . $row['id'] . '#comments">' .
$row['num_comments'] . ' comments</a>';
echo '<hr/>';
}
}
$DBH = null;
echo <<<HTML
<a href="post_add.php">+ New Post</a>
HTML;
?>
它不显示任何内容,甚至不显示错误代码。我能够用mysql_uu语句正确地执行它,但我真的想学习如何正确地执行它。我只是在寻找一个正确的方向,你现在必须为我编码。提前谢谢你的帮助 让我们从头开始 如果不传递任何参数,则不使用准备好的语句 这是什么意思?只需发出PDO的
query
函数
是这样的:
$query = $DBH->query('SELECT * FROM blog_post ORDER BY date_posted DESC');
如果出现错误,$DBH->query将在成功时返回FALSE或pdo语句。所以下一行是检查它是否为假:
if($query === false)
{
echo $DBH->errorCode();
}
else
{
// Everything went fine, let's fetch results
$results = $query->fetchAll(PDO::FETCH_ASSOC);
// If there are any records returned, our array won't be empty.
if(count($results))
{
// $results contains all of your records. You can now loop it with for, foreach and you don't need a while loop anymore
foreach($results as $row)
{
print_r($row);
}
}
else
{
echo "There are no records in the database table :(";
}
}
您使用了
prepare
,但没有使用execute
。如果不使用PDOStatement::execute
,则无法获取某些内容。在您的例子中,它是$blogIndexstmt->execute()
。除了@N.B.编写的内容之外,您还使用MySQL语句中的$row。现在,您需要使用$blogIndexRows['title']作为例子$行未在任何位置定义。