How can a dropdown menu be populated with data from a MySQL database in PHP?

To populate a dropdown menu with data from a MySQL database in PHP, you can query the database to retrieve the data and then loop through the results to generate the options for the dropdown menu. You can use the mysqli or PDO extension in PHP to connect to the database and fetch the data.

<?php
// Connect to the database
$connection = new mysqli("localhost", "username", "password", "database");

// Check connection
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

// Query to fetch data from database
$query = "SELECT id, name FROM table";
$result = $connection->query($query);

// Populate dropdown menu with data
echo "<select>";
while ($row = $result->fetch_assoc()) {
    echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";

// Close database connection
$connection->close();
?>