What are the potential pitfalls of using PHP to dynamically generate options for a select element from a database query?

One potential pitfall of using PHP to dynamically generate options for a select element from a database query is the risk of SQL injection if the input is not properly sanitized. To prevent this, always use prepared statements when executing database queries in PHP to avoid malicious code injection.

// Connect to the database
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Prepare and execute the query
$stmt = $pdo->prepare("SELECT id, name FROM options_table");
$stmt->execute();

// Generate options for select element
while ($row = $stmt->fetch()) {
    echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}