I am a newcomer in php and mysql. I am trying to display posts from my database as month and year with also counting total post in a month. my code is here...
<?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 } ?>
But i am not getting correct result i can only see brakets and within the braket total number of posts like (7) Can anyone please help me?
you have a typo in your query, EAR instead of YEAR. The error tells you just that.
<?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 } ?>
See the part:
YEAR(FROM_UNIXTIME(post_date)) AS YEAR
Also from your website, it seems that a href is not correct. Try this instead:
<a href="<?php echo 'posts.php?month='.$row['MONTH']; ?>">