我是php和mysql的新手。我试图在月份和年份显示我的数据库中的帖子,并计算一个月内的总帖子数。我的代码在这里......
<?php
$sql="SELECT YEAR(FROM_UNIXTIME(post_date)) AS EAR,MONTHNAME(FROM_UNIXTIME(post_date)) AS MONTH, COUNT(*) AS TOTAL from post GROUP BY YEAR, MONTH";
$result=mysql_query($sql,$bd) or die("Error:".mysql_error());
while($row=mysql_fetch_array($result)){
?>
<dl>
<dt><?php echo $row['YEAR']; ?></dt>
<dd><a href="posts.php?month=<?php echo $row['MONTH']; ?>"><?php echo " ".$row['MONTH']."(".$row['TOTAL'].")" ?></a></dd>
</dl>
<?php } ?>
但是我没有得到正确的结果我只能看到brakets和braket总帖子中的数量如(7) 有人可以帮帮我吗?
答案 0 :(得分:0)
您的查询中有拼写错误,而不是YEAR。错误告诉你这个。
<?php
$sql="SELECT YEAR(FROM_UNIXTIME(post_date)) AS YEAR,MONTHNAME(FROM_UNIXTIME(post_date)) AS MONTH, COUNT(*) AS TOTAL from post GROUP BY YEAR, MONTH";
$result=mysql_query($sql,$bd) or die("Error:".mysql_error());
while($row=mysql_fetch_array($result)){
?>
<dl>
<dt><?php echo $row['YEAR']; ?></dt>
<dd><a href="posts.php?month=<?php echo $row['MONTH']; ?>"><?php echo " ".$row['MONTH']."(".$row['TOTAL'].")" ?></a></dd>
</dl>
<?php } ?>
参见部分:
YEAR(FROM_UNIXTIME(post_date)) AS YEAR
同样来自您的网站,似乎href不正确。试试这个:
<a href="<?php echo 'posts.php?month='.$row['MONTH']; ?>">