How can PHP be used to create dynamic dropdown menus based on MySQL data?

To create dynamic dropdown menus based on MySQL data using PHP, you can query the database to retrieve the data and then use a loop to generate the options for the dropdown menu. You can use the fetched data to populate the dropdown menu dynamically.

<?php
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Query to fetch data from MySQL
$query = "SELECT id, name FROM options_table";
$result = mysqli_query($connection, $query);

// Generate dropdown menu options
echo '<select name="dropdown">';
while($row = mysqli_fetch_assoc($result)) {
    echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
}
echo '</select>';

// Close database connection
mysqli_close($connection);
?>