I need someone to assist in finding out the error in this or correct the codes. I want the codes to give me years in the publicationdate from my DB. However, though I am getting the years, I am getting a particular year repeated. If I put journals in for every month of the year 2012 and I call this code, I will have 2012 repeated 12 times. My intention is just to have 2012 just printed once. I hope someone can help to correct this please.
<?php
//to connect to the database
function db_connect()
{
$result = mysql_pconnect('localhost', 'alayande', 'nicholas');
if (!$result)
return false;
if (!mysql_select_db('topclass_journals'))
return false;
return $result;
}
$conn = db_connect();
if (!$conn)
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
mysql_select_db('topclass_journals');
$query = "select publicationdate from agricultural_research_journal order by id desc";
$result = mysql_query($query);
$num_result = mysql_num_rows($result);
//to pick the dates
for ($i=0; $i <$num_result; ++$i)
{
$story = mysql_fetch_array($result);
print '<table width = "385" border="1" align = "center">';
print '<tr>';
print '<td>';
$year = $story['publicationdate'];
$date = $year;
$my_date = date('Y', strtotime($date));
echo "<a href=page.php?year=$my_date>$my_date</a>";
print '</td>';
print '</tr>';
print '';
print '<tr><td align="right">';
print '</table>';
}
?>