2

i have this total from my first table (income) and I would want to subtract it from the another table(exp) query. All these code are on thesame page. So how can i use these query variables to subtract and echo result on thesame page but in another location? Query 1

<?php 
include("db.php");
$query = "SELECT source, SUM(income.inamount) FROM income"; 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "Total Sales  = N". $row['SUM(inamount)'];
echo "<br />";
}
?>


<?php
include("db.php");
$query = "SELECT amount, SUM(exp.amount) FROM exp"; 

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "Total Expenditure  = N". $row['SUM(amount)'];
echo "<br />";
}
?>
1
  • in another location means in another file? Commented Oct 2, 2015 at 14:52

3 Answers 3

1

Check that the data type is set to int.

$myVar = $row['SUM(inamount)'] - $row['SUM(amount)'];
echo $myVar;

Another approach is to assign the two values to their own variable then perform the calculation:

$totalSales = $row['SUM(inamount)'];
$expense = $row['SUM(amount)'];

$actual = $totalSales - $expense;

echo $actual;

Using var_dump() will tell you what values are actually being held in the $row Variables. This will give you clues as to whether the data is getting pulled from the database into the variables properly.

var_dump($row['SUM(inamount)']);
var_dump($row['SUM(amount)']);

Another thing to check is if the actual variables are being set so overall the solution might look like this:

<?php 
include("db.php");

//query 1
$query_income = "SELECT source, SUM(income.inamount) FROM income"; 
$result_income = mysql_query($query_income) or die(mysql_error());
while($row = mysql_fetch_array($result_income)){
$inamount = $row['inamount'];
}

//query 2
$query_exp = "SELECT amount, SUM(exp.amount) FROM exp"; 
$result_exp = mysql_query($query_exp) or die(mysql_error());
while($row = mysql_fetch_array($result_exp)){
$amount = $row['amount'];
}

if(isset($inamount, $amount))  {
 $actual = $amount - $inamount;
 echo $actual // obviously you format $actual to your preferred     output
 }
 else {
   echo "Variable data not set";
 }
?>
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks for the Answer but there is a little conflict here; Total income table column is 7000 while Total exp table column 3000. From your solution the result returned 0. i.e $row['SUM(inamount)'] is 7000 while $row['SUM(amount)'] is 3000
Pls, Did Mysql Data Type have a role to play here. Because all this solutions are giving me 0 as the result. The two columns from those tables are Varchar as data types so is that correct?
$totalSales = $row['SUM(inamount)']; $expense = $row['SUM(amount)']; $actual = $totalSales - $expense; echo $actual; Returns wrong result even as Data Type is set to INT
Did you change your data type to int?
Yes, in all the two tables holding the figures
|
0

Assign the value of $row['SUM(inamount)'] to a variable after echoing total sales, which you can use to perform the subtraction:

...
$var=$row['SUM(inamount)'];
...
//after printing total expenditure
echo "Total Profit  = ". ($var-$row['SUM(amount)']);

2 observations, though: 1. mysql libary is already deprecated, use mysqli or PDO instead. 2. There is no need to include db.php twice, if the 2 pieces of code are in the same page.

5 Comments

Thanks for your answer. Please explain a little i don't understand.
What is exactly that you do not understand?
Sorry, I got it. But those columns what should be there exact data types for accurate result?
Have updated my answer. I would set the data type to int
Numeric values should be stored numbers, not as text, so use one of the integer data types or decimal if you need to store decimals as well.
0

My best suggestion should be to make just one query and process the substraction within the query, you can make it as follow:

<?php 
include("db.php");
$query = "SELECT inc.source, SUM(inc.inamount) as in_amount, SUM(exp.amount) as out_amount, SUM(inc.inamount)-SUM(exp.amount) as total FROM income as inc, exp"; 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "Total Sales  = N". $row['in_amount'];
echo "<br />";
echo "Expenditure Sales  = N". $row['out_amount'];
echo "<br />";
echo "Total after substraction  = N". $row['total'];
echo "<br />";
}
?>

But if you strictly want to make the substraction with the variables, then you can make it as follow:

<?php 
include("db.php");
$query = "SELECT source, SUM(inamount) as in_amount FROM income"; 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
$total_sales = $row['in_amount'];
echo "Total Sales  = N". $row['in_amount'];
echo "<br />";
}
?>

<?php
include("db.php");
$query = "SELECT SUM(amount) as out_amount FROM exp"; 

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
$total_expenditure = $row['out_amount'];
echo "Total Expenditure  = N". $row['out_amount'];
echo "<br />";
}
?>

<?php
$total = $total_sales - $total_expenditure;
echo "Total after substract = N". $total;
echo "<br />";
?>

Is up to you what option you would like to use, but i suggest not to make two different queries to get two values, because they could change within a timeframe.

3 Comments

Be aware of the datatypes in your database, what you can do is add a new column to each table, for example inamount_int and amount_int, then you need to cast the varchar columns to int and save the int value to the new columns created. To cast one of them you can do UPDATE income SET inamount_int = cast(inamount as UNSIGNED);
You're welcome. Please remember to choose the best answer, so it can help other people know which one was the best solution
The best Answer is that of Jesus Flores. "My best suggestion should be to make just one query and process the substraction within the query. Although all his two answers worked well but the first one is more effective as he said. Thanks

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.