Leo Leo - 4 years ago 84
MySQL Question

populate drop down list from mysql database and don't repeat values

I am populating a drop down menu from mysql database. It works well, But I want it not to repeat values. (i.e if some value is N times in database it comes only once in the drop down list)

Here is my code:

<?php

mysql_connect('host', 'user', 'pass');
mysql_select_db ("database");

$sql = "SELECT year FROM data";
$result = mysql_query($sql);

echo "<select name='year'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['year'] . "'>" . $row['year'] . "</option>";
}
echo "</select>";

?>

Answer Source

Use DISTINCT in your query.

"SELECT DISTINCT year FROM data";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download