0

I have a database called class_database. I have connection to my database through my php file. I believe my query is correct, however it will not populate into html dropdown menu. Listed below are the two sections of php I have written with the html dropdown menu included.

<?php
$mysqli= NEW MySQLi ('localhost' , 'root' , '' , 'class_database');
$resultSet = $mysqli->query( "SELECT * FROM 'class_list' ORDER BY 'ClassNumber' DESC ");
?>
<select name="class_list">
<?php
while($rows = $resultSet->fetch_assoc())
{
  $classnumbers = $rows['classnumbers'];
  echo "<option value='classnumbers'>$classnumbers</option>";
}
?>
</select>

The above code results in a blank dropdown menu. How do I properly populate the dropdown menu?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    _Side note:_ PHPMyAdmin is just a web based admin tool for managing MySQL/MariaDB databases. Your application talks directly to the database and doesn't have anything to do with PHPMyAdmin. – M. Eriksson Apr 08 '21 at 14:30
  • 1
    If it's blank, then the loop was never entered. Are you sure your query should return data? – El_Vanja Apr 08 '21 at 14:33
  • 2
    `ORDER BY 'ClassNumber'` should either be `ORDER BY ClassNumber` or `ORDER BY \`ClassNumber\`` (back ticks, not single quotes). Same issue with `FROM 'class_list'`. However, you don't need to put the column names in back ticks unless they are reserved words in MySQL. – M. Eriksson Apr 08 '21 at 14:34
  • 2
    Aside from that, what is the point of creating *n* options with the same value? I believe you wanted to insert the variable there as well. – El_Vanja Apr 08 '21 at 14:35
  • 2
    If some code which includes a database query doesn't work, I would recommend that you always start by testing the query in PHPMyAdmin (since you're using that) to see if it is correct or not, even if you believe it is. – M. Eriksson Apr 08 '21 at 14:39
  • @MagnusEriksson I believe the back ticks helped. Now the dropdown menu actually drops down longer than it did. I have a new error stating that I have undefined indexes. This would be on the $classnumbers = $rows ['classnumbers'] line – BogusRustyRuntime Apr 08 '21 at 14:45
  • 2
    Start by reading [this](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined). – El_Vanja Apr 08 '21 at 14:46
  • You don't actually need the back ticks at all in the above query. Neither `class_list` or `ClassNumber` are reserved words in MySQL. – M. Eriksson Apr 08 '21 at 14:48
  • oh okay I misread. It also worked without because as you said they aren't reserved words. – BogusRustyRuntime Apr 08 '21 at 14:49
  • Technically the question was answered. The menu is now populated but not displaying the data properly. On to more troubleshooting! Thanks for the help you guys – BogusRustyRuntime Apr 08 '21 at 14:53

0 Answers0