Alesfatalis
Alesfatalis

Reputation: 777

php sum of an sql query and an php variable

Hey guys i want a sum of an mysql query and an php variable. Is there a function to do this? Here is what i tried:

$result3 = mysql_query($sql3);
                while ($resultarray3 = mysql_fetch_array($result3)){
                $Ergebnis = $Menge + $resultarray['Bestand'];
                echo $Ergebnis;
                }

Can anyone help me on this?

Edit: I want a sum of an php variable and an mysql query not of two sql tables!!!!

Upvotes: 0

Views: 277

Answers (4)

dzgok
dzgok

Reputation: 26

$menge = '';
$result3 = mysql_query($sql3);
while ($resultarray3 = mysql_fetch_array($result3)){
  $menge = $menge + $resultarray3['Bestand'];
}
// result => $menge

Upvotes: 1

tobiv
tobiv

Reputation: 842

You define $resultarray3 but then you use $resultarray without the three.

$Ergebnis = $Menge + $resultarray3['Bestand'];

Upvotes: 0

v0d1ch
v0d1ch

Reputation: 2748

If there is only one row you don't need the .=

$result3 = mysql_query($sql3);
            while ($resultarray3 = mysql_fetch_array($result3)){
            $Ergebnis .= $Menge + $resultarray3['Bestand'];//notice the change on this line
            echo $Ergebnis;
            }

Upvotes: 1

Hugo Delsing
Hugo Delsing

Reputation: 14173

A query to show a result contains SUM(column), so you could use:

$sql3 = "SELECT bestand, SUM(bestand) as menge FROM database GROUP BY bestand";

And then in PHP

$result3 = mysql_query($sql3);
while ($resultarray3 = mysql_fetch_array($result3)){
    echo $resultarray['bestand'] . ' = ' . $resultarray['menge'];
}

Upvotes: 0

Related Questions