What is the best way to display database query results in a dropdown menu in PHP?

To display database query results in a dropdown menu in PHP, you can fetch the data from the database, loop through the results, and create option elements for each record to populate the dropdown menu. You can use the mysqli or PDO extension to connect to the database and retrieve the data. Finally, echo out the HTML code for the dropdown menu with the populated options.

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

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

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

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

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