简体   繁体   中英

How to add every last record on the database? E.g. 1 += 1

So, Im fetching the last record on the table "energy"

function find_all_energy() {
    global $db;

    $sql = "SELECT * FROM energy ORDER BY energyID DESC";
    $result = mysqli_query($db, $sql);
    $energy = mysqli_fetch_assoc($result);
    mysqli_free_result($result);

    return $energy;
}

The last record returns "1000", but what if there are new inserted record(s) like "2000", I want to return it as "3000" because of ( += ) and new inserted record(s) again like "5000" then it will fetch it as "8000" and so on. Thank you!

What you what is the last line of your table ? I would suggest to do it with a MAX(id) on a WHERE requirement, like it is suggested in this post :

SELECT row 
FROM table 
WHERE id=(
    SELECT max(id) FROM table
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM