What are the potential pitfalls of structuring a database query in PHP for sending personalized emails to workshop participants?

One potential pitfall of structuring a database query in PHP for sending personalized emails to workshop participants is not properly sanitizing user input, which can lead to SQL injection attacks. To solve this issue, it is important to use prepared statements or parameterized queries to prevent SQL injection vulnerabilities.

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

// Prepare a statement to retrieve participant information
$stmt = $pdo->prepare('SELECT email, first_name FROM participants WHERE workshop_id = :workshop_id');
$stmt->bindParam(':workshop_id', $workshop_id, PDO::PARAM_INT);
$stmt->execute();

// Loop through results and send personalized emails
while ($row = $stmt->fetch()) {
    $to = $row['email'];
    $subject = 'Personalized Workshop Email';
    $message = 'Dear ' . $row['first_name'] . ', Thank you for participating in our workshop!';
    // Send email using mail() function or PHPMailer
}