How can PHP be used to store and retrieve security question answers in a database efficiently?

To efficiently store and retrieve security question answers in a database using PHP, you can use prepared statements to prevent SQL injection attacks and securely store the answers in the database. When retrieving the answers, make sure to validate the input to prevent any malicious attacks.

// Store security question answers in the database
$stmt = $pdo->prepare("INSERT INTO security_answers (user_id, question_id, answer) VALUES (?, ?, ?)");
$stmt->execute([$user_id, $question_id, $answer]);

// Retrieve security question answers from the database
$stmt = $pdo->prepare("SELECT answer FROM security_answers WHERE user_id = ? AND question_id = ?");
$stmt->execute([$user_id, $question_id]);
$answer = $stmt->fetchColumn();