所以我一直在使用 PHP 通过 MySQL 列出我网站的文章。问题是,帖子应该在顶部列出最新的帖子,但最旧的帖子却在顶部。我该如何改变这一点。下面是一段源代码:
<?php foreach ($posts as $post): ?>
<div class="container">
<h2 class="title"><a href=""><?php echo htmlspecialchars($post['title'], ENT_QUOTES, 'UTF-8'); ?></a></h2>
<p class="date"><?php echo htmlspecialchars($post['date'], ENT_QUOTES, 'UTF-8'); ?></p>
<div class="body">
<p><?php echo htmlspecialchars($post['content'], ENT_QUOTES, 'UTF-8'); ?></p>
</div>
</div>
<?php endforeach; ?>
try {
$sql = 'SELECT id, title, date, content FROM post';
$result = $pdo->query($sql);
} catch (PDOException $e) {
$error = 'Error connecting to database server: ' . $e->getMessage();
exit();
}