How can PHP be used to display new forum posts for users?

To display new forum posts for users using PHP, you can retrieve the posts from a database sorted by the timestamp in descending order. You can then loop through the retrieved posts and display them on the forum page for users to see.

<?php
// Connect to your database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "forum";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve new forum posts sorted by timestamp in descending order
$sql = "SELECT * FROM posts ORDER BY timestamp DESC";
$result = $conn->query($sql);

// Display the new forum posts
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<div>";
        echo "<h3>" . $row["title"] . "</h3>";
        echo "<p>" . $row["content"] . "</p>";
        echo "<p>Posted on: " . $row["timestamp"] . "</p>";
        echo "</div>";
    }
} else {
    echo "No new posts found.";
}

$conn->close();
?>