简体   繁体   中英

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!!!!

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

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;
            }

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'];
}

你定义$resultarray3然后你使用$resultarray而没有三个。

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

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