-1

I'm in the process of putting together a PHP/mySQL form with only basic knowledge of both. For the first drop-down list on the form, I'd like to pull the options from a mySQL database. I've set this up as follows:

<?php
$query = "SELECT * FROM test";
$result = mysqli_query($link , $query);
?>
Select your group number:
<select name="group">
<?php
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['group'];?>"><?php echo $line['group'];?></option>
<?php
}
?>
</select>

However, this returns each group value even when they're identical, so I end up with duplicates (FIGURE 1). I'd like to group all like values (FIGURE 2).

enter image description here

I understand using GROUP BY might accomplish this, but when I include GROUP BY in my code as follows, my drop-down just ends up empty.

$query = "SELECT * FROM test GROUP BY group";

Am I making a mistake with my use of GROUP BY?

NOTE: "group" is the id of the column I'm fetching.

Marcatectura
  • 1,721
  • 5
  • 30
  • 49

1 Answers1

1

group is not a good name for a field because it is a reserved word in mysql.

You can do it but your query needs to look like this:

$query = "SELECT * FROM test GROUP BY `group`";

Please read this: How do I escape reserved words used as column names? MySQL/Create Table

and this: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

Community
  • 1
  • 1
steven
  • 4,868
  • 2
  • 28
  • 58