Dadan
Dadan

Reputation: 1

How to get total from previous record in php mysql

I have a problem with this:

count this id no 1 = usage/nod
count this id no 2 = (usage[id1]+usage[id2])/(nod[id1]+nod[id2])
count this id no 3 = (usage[id1]+usage[id2]+usage[id3])/(nod[id1]+nod[id2]+nod[id3])

And so on...

"usage/nod" is a field in the database.

How can I count that with PHP and MySQL?

Upvotes: 0

Views: 68

Answers (1)

Barmar
Barmar

Reputation: 782775

$total_usage = 0;
$total_nod = 0;

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $total_usage += $row['usage'];
    $total_nod += $row['nod'];
    $div = $total_usage/$total_nod;
    echo "count this id no $row[id] = $div\n";
}

Upvotes: 1

Related Questions