Chathurika Chathurika - 5 months ago 16
MySQL Question

Pull DB data into drop down in MYSQL, PHP

I want to make select option by getting DB values into a drop down, following is my code that I have used,

$query = "SELECT apply_year FROM emp_leaves ORDER BY apply_year DESC";
$result = mysql_query($query);
echo "<select name='apply_year' class=\"form-control\"><option>Select Year</option>";
while ($r = mysql_fetch_array($result)) {
echo "<option value=" . $r['leave_id'] . ">" . $r['apply_year'] . "</option>";
}
echo "</select>"; ?>


It work fine, But I want to show only one value of each years, I want to avoid repeat same year in drop down. Please help

Answer

Use DISTINCT for apply_year and Create mysql query like, so that it will return apply_year only once in result:

$query = "SELECT DISTINCT apply_year FROM emp_leaves ORDER BY apply_year DESC";
Comments