How can PHP code be structured to display guestbook entries in reverse order?

To display guestbook entries in reverse order, you can retrieve the entries from the database in descending order based on the entry timestamp. This way, the latest entry will be displayed first. You can achieve this by using an SQL query with the ORDER BY clause in PHP.

// Connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);

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

// Retrieve guestbook entries in reverse order
$sql = "SELECT * FROM guestbook_entries ORDER BY entry_timestamp DESC";
$result = $conn->query($sql);

// Display the entries
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Name: " . $row["name"]. " - Message: " . $row["message"]. "<br>";
    }
} else {
    echo "No entries found";
}

// Close the connection
$conn->close();