What are some possible solutions to prevent the subtraction of old values from the database when a user refreshes the page in PHP?

When a user refreshes a page in PHP, the old values from the database may be subtracted because the page reloads and fetches data again. To prevent this, one solution is to use sessions to store the fetched data and check if it already exists before making another database query. This way, the old values will not be subtracted when the page is refreshed.

<?php
session_start();

if(!isset($_SESSION['data'])) {
    // Perform database query to fetch data
    $data = fetchDataFromDatabase();

    // Store fetched data in session
    $_SESSION['data'] = $data;
} else {
    // Use data from session instead of querying the database again
    $data = $_SESSION['data'];
}

// Display data on the page
foreach($data as $row) {
    echo $row['column_name'];
}
?>