Are there alternative methods to using a while loop to populate a dropdown menu in PHP with values from a MySQL table?

To populate a dropdown menu in PHP with values from a MySQL table without using a while loop, you can use the fetchAll() method to retrieve all rows from the database at once and then iterate over the results using a foreach loop to create the options for the dropdown menu.

<?php
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');

// Fetch all rows from the table
$stmt = $pdo->query('SELECT * FROM table_name');
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Create the dropdown menu
echo '<select name="dropdown">';
foreach ($rows as $row) {
    echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
}
echo '</select>';
?>