我正在尝试在旁边显示包含邮资日期的新闻标题。出于某种原因,每个人的日期是相同的;我没有在同一天发布所有这些内容,因为数据库中的日期各不相同。
这是我的代码:
<?php
// Connect to the database
$dbLink = new mysqli('localhost', 'root', 'root', 'olearyinternational');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Query for a list of all existing files
$sql = 'SELECT * FROM `posts` order by created desc limit 8';
$result = $dbLink->query($sql);
$date = date('jS F Y', $d);
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
while($row = $result->fetch_assoc()) {
?>
<li class="good-font"><a href="news.php"><?php echo $row['title'] ?><span id = "post_date"><?php echo $date; ?></a></li>
<?php
}
// Close table
echo '</table>';
// Free the result
$result->free();
}
}
else {
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
?>
答案 0 :(得分:2)
您的$ date会在您查询的位置初始化一次。您需要在每行而不是$ date中回显postdate。检查你的架构,它可能类似于$ row ['date']或$ row ['post_date']。如果您需要更改格式,请在while循环中执行。