Bjaeg Bjaeg - 6 months ago 24
MySQL Question

SQL SELECT DISTINCT Not functioning

I have a selector were I wish users to only see distinct 'items' from a table. Below I have code to loop through the results. If I change the query to SELECT * it works perfectly fine.

<select name="sample">
<option value="trail">Trail</option>
<?php
$set = $mysqli->query("SELECT DISTINCT set FROM trail ORDER BY category");
if ($set->num_rows != 0){
while($set_1 = $set->fetch_assoc()) {
$set_category = $set_1['category'];
$set_set = $set_1['set'];
?>
<OPTION VALUE="<?php echo $set_set; ?>"><?php echo $set_category.' :: '.$set_set ?></OPTION>
<?php
}
}
?>
</select>


Is DESTINCT depreciated? I can't figure out why it wouldn't work.

I have come here after checking other peoples questions regarding this topic. I Tried adding two columns and that displayed nothing as well.

Answer

set is a reserve word and so needs escaping like below. Moreover, refrain yourself from using any reserve word (or) key word as your table column name.

SELECT DISTINCT `set` FROM trail ORDER BY category