How can PHP be used to retrieve and display user comments on specific user profiles while ensuring data integrity and user privacy?

To retrieve and display user comments on specific user profiles while ensuring data integrity and user privacy, you can use PHP to query the database for comments associated with the specific user profile and then display them securely by sanitizing the input data to prevent SQL injection and XSS attacks.

// Assuming you have a database connection established

// Retrieve user comments from the database
$user_id = $_GET['user_id']; // assuming user_id is passed in the URL
$query = "SELECT * FROM comments WHERE user_id = ?";
$stmt = $pdo->prepare($query);
$stmt->execute([$user_id]);
$comments = $stmt->fetchAll();

// Display user comments
foreach ($comments as $comment) {
    $comment_text = htmlspecialchars($comment['comment']); // Sanitize comment text
    echo "<div>{$comment_text}</div>";
}