Hi,

Thanks for checking this out.

My goal is simple. Create a list of all the years that have had articles published in them (so basically every year). I have three unique dates in the date column, 2008-08-20, 2007-08-20, and 2006-08-20.

The code below only returns this as the print_r output;
Array ( [year] => 2008 )

$sql = "SELECT DISTINCT year(date) AS 'year' FROM news ";
$year_query = mysql_query($sql);
$years = mysql_fetch_assoc($year_query);

print_r($years);


foreach ($years as $year){
	echo '<p>' .$year. '</p>';
}

Any help would be great as I have looked all over the net for this.


Thanks again.

It's all here: http://nl2.php.net/mysql_fetch_assoc

$sql = "SELECT DISTINCT year(date) AS 'year' FROM news ";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
  echo '<p>' . $row['year'] . '</p>';
}
commented: Great help. Solved problem. Thanks +1

pritaeas,

Thank you very much. I had read that page multiple times trying to figure this out. I thought it was in the SQL statement as when I created an array manually with three values and used the foreach loop on it there were no problems.

Anyway thanks for taking the time. I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.