richrock Posted April 3, 2008 Share Posted April 3, 2008 Help!!! I'm currently writing an admin panel, and need to extract two results from two tables and calculate a difference between the two. The layout is : 1: The sum of: tbl_bids, row_bidvalue. 2: The sum of: tbl_sell, row_sold. I can get the individual values using this: // Get the auction price values $query_sell = "SELECT userid, SUM(initial_price) FROM jos_bid_auctions WHERE userid = ".$uid." GROUP BY userid"; $result_sell = mysql_query($query_sell) or die(mysql_error()); // Get the bidded values $query_bid = "SELECT userid, SUM(bid_price) FROM jos_bids WHERE userid = ".$uid." GROUP BY userid"; $result_bid = mysql_query($query_bid) or die(mysql_error()); But can't for the life of me figure out how to get a difference from each result. I tried placing these in functions and call the functions as part of a sum : $first_res = $result1; $second_res = $result2; echo "here"; echo $first_res; $diff_total = $first_res - $second_res; echo $diff_total; But it doesn't work. Sorry but I am a newbie at this. I've only started doing PHP and instead of 'Hello World' I get an admin panel to code Rich Link to comment https://forums.phpfreaks.com/topic/99349-difference-between-results-on-two-tables/ Share on other sites More sharing options...
Orio Posted April 3, 2008 Share Posted April 3, 2008 Not really math based but whatever. Try this: <?php // Get the auction price values $query_sell = "SELECT userid, SUM(initial_price) FROM jos_bid_auctions WHERE userid = ".$uid." GROUP BY userid"; $result_sell = mysql_query($query_sell) or die(mysql_error()); list($sell_sum) = mysql_fetch_assoc($result_sell); // Get the bidded values $query_bid = "SELECT userid, SUM(bid_price) FROM jos_bids WHERE userid = ".$uid." GROUP BY userid"; $result_bid = mysql_query($query_bid) or die(mysql_error()); list($bid_sum) = mysql_fetch_assoc($result_bid); echo "{$sell_sum} - {$bid_sum} = ".($sell_sum - $bid_sum); ?> Orio. Link to comment https://forums.phpfreaks.com/topic/99349-difference-between-results-on-two-tables/#findComment-508353 Share on other sites More sharing options...
Barand Posted April 3, 2008 Share Posted April 3, 2008 If you are using list(), you need an indexed array (ie mysql_fetch_row) Link to comment https://forums.phpfreaks.com/topic/99349-difference-between-results-on-two-tables/#findComment-508725 Share on other sites More sharing options...
Orio Posted April 4, 2008 Share Posted April 4, 2008 If you are using list(), you need an indexed array (ie mysql_fetch_row) Really? I didn't know that... So if that's the case, you can do it like this: <?php // Get the auction price values $query_sell = "SELECT userid, SUM(initial_price) AS s1 FROM jos_bid_auctions WHERE userid = ".$uid." GROUP BY userid"; $result_sell = mysql_query($query_sell) or die(mysql_error()); $tmp = mysql_fetch_assoc($result_sell); $sell_sum = $tmp['s1']; // Get the bidded values $query_bid = "SELECT userid, SUM(bid_price) AS s2 FROM jos_bids WHERE userid = ".$uid." GROUP BY userid"; $result_bid = mysql_query($query_bid) or die(mysql_error()); $tmp = mysql_fetch_assoc($result_bid); $bid_sum = $tmp['s2']; echo "{$sell_sum} - {$bid_sum} = ".($sell_sum - $bid_sum); ?> Link to comment https://forums.phpfreaks.com/topic/99349-difference-between-results-on-two-tables/#findComment-509136 Share on other sites More sharing options...
richrock Posted April 25, 2008 Author Share Posted April 25, 2008 Thanks guys, I wasn't going to use list(), but I'll put them in the code and see what happens. Rich Link to comment https://forums.phpfreaks.com/topic/99349-difference-between-results-on-two-tables/#findComment-526897 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.