0

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

5
  • 2
    What is your sql query? There is a SUM() function in MySQL that may server this purpose more efficiently, depending on how/what you're using. Commented Dec 3, 2012 at 14:23
  • @newfurniturey read my edit Commented Dec 3, 2012 at 14:26
  • $Ergebnis .= $Menge + $resultarray['Bestand']; ? Commented Dec 3, 2012 at 14:28
  • @2-Stroker @vodich I was looking for something which does this: $Ergebnis .= $Menge + $resultarray['Bestand']; because when i use my solution $Ergebnis is the same as $menge Commented Dec 3, 2012 at 14:30
  • I noticed that you use $resultarray['Bestand'] instead of $resultarray3['Bestand'] Commented Dec 3, 2012 at 14:31

4 Answers 4

1
$menge = '';
$result3 = mysql_query($sql3);
while ($resultarray3 = mysql_fetch_array($result3)){
  $menge = $menge + $resultarray3['Bestand'];
}
// result => $menge
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

1 Comment

no i want simply do this: $Ergebnis .= $Menge + $resultarray['Bestand']; and $menge is an php variable
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.