What is the best practice for displaying new forum posts for individual users in PHP?

When displaying new forum posts for individual users in PHP, the best practice is to store the user's last viewed post ID in the database and then retrieve and display all posts with a higher ID than the user's last viewed post. This ensures that the user only sees new posts since their last visit.

// Assuming $lastViewedPostId is the last viewed post ID stored in the database for the user
// Retrieve and display new forum posts for the user
$sql = "SELECT * FROM forum_posts WHERE post_id > $lastViewedPostId";
$result = mysqli_query($connection, $sql);

if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        // Display the new forum posts
        echo $row['post_content'];
    }
} else {
    echo "No new posts to display.";
}