What potential pitfalls should be considered when using SQL queries to populate dropdown menus in PHP?

When using SQL queries to populate dropdown menus in PHP, it is important to consider the potential risk of SQL injection attacks. To prevent this, it is recommended to use prepared statements with parameterized queries to sanitize user input and prevent malicious code execution.

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');

// Prepare a SQL query using a parameterized statement
$stmt = $pdo->prepare("SELECT id, name FROM dropdown_options WHERE category = :category");

// Bind the parameter value
$stmt->bindParam(':category', $category, PDO::PARAM_STR);

// Execute the query
$stmt->execute();

// Populate the dropdown menu with the results
echo "<select>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";