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!

Cyril Mia
  • 31
  • 1
  • 5
  • 1
    If you need the sum of a column for all rows then simply use `SUM(columnName)`. If that's not the case add some data and the expected output. – Peter M Mar 16 '18 at 15:56
  • I need to add the data within the date e.g. only in March 16, 2018 data will be += from the current data. Then if it becomes march 17, then it will add the data within its date – Cyril Mia Mar 16 '18 at 16:05
  • Please provide a sample of your data and the desired result. – Peter M Mar 16 '18 at 18:17

1 Answers1

0

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
    )
fictimaph
  • 106
  • 6