What is the best practice for dynamically populating a dropdown menu in PHP from a database?

When dynamically populating a dropdown menu in PHP from a database, the best practice is to first establish a connection to the database, retrieve the data needed for the dropdown menu, and then loop through the data to generate the options for the dropdown.

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

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

// Retrieve data from the database
$query = "SELECT id, name FROM options_table";
$result = $connection->query($query);

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

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