matt.sisto Posted May 6, 2009 Share Posted May 6, 2009 I want to use the results from my first sql query to define a variable. Basically I am selecting the months bookings but I want to add the fees for each booking together to create a total, unfortunately I am failing miserably. Any help please, much appreciated. <?php require "dbconn2.php"; $org_id = $_POST["org_id"]; $month1 = $_POST["month1"]; $day1 = $_POST["day1"]; $year1 = $_POST["year1"]; $month2 = $_POST["month2"]; $day2 = $_POST["day2"]; $year2 = $_POST["year2"]; $invoice_start = $year1."-".$month1."-".$day1." ".$_POST["event_start"]; $invoice_end = $year2."-".$month2."-".$day2." ".$_POST["event_end"]; if ($org_id !=null){ $sql = "SELECT event_id, event_start, event_end, service_id, unit, quantity, fee FROM calendar_events CROSS JOIN client USING (client_id) WHERE calendar_events.event_start >= '".$invoice_start."' AND calendar_events.event_end <= '".$invoice_end."' AND client.org_id = '".$org_id."'"; $result1 = mysql_query ($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()) if($result1 != null){ $result2 = mysql_query("SELECT SUM(fee) FROM '$result1'"); if($result2 !=null){ $totalFee = mysql_result($result2, 0); } } } echo $totalFee; ?> <html> <head> <title>Invoice</title> </head> <body> </body> </html> Link to comment https://forums.phpfreaks.com/topic/157090-solved-using-results-from-an-sql-query-adding-them-together-to-define-a-variable/ Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 You do realize $result1 is a mysql resource variable right? So in your second SQL, using it as a tablename doesn't make sense. Link to comment https://forums.phpfreaks.com/topic/157090-solved-using-results-from-an-sql-query-adding-them-together-to-define-a-variable/#findComment-827477 Share on other sites More sharing options...
matt.sisto Posted May 6, 2009 Author Share Posted May 6, 2009 Well, I'm still learning, so its a bit of trial and error. How can I make this work? Link to comment https://forums.phpfreaks.com/topic/157090-solved-using-results-from-an-sql-query-adding-them-together-to-define-a-variable/#findComment-827480 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 I don't even know. If you have tables stored that way, then I'm baffled. Link to comment https://forums.phpfreaks.com/topic/157090-solved-using-results-from-an-sql-query-adding-them-together-to-define-a-variable/#findComment-827486 Share on other sites More sharing options...
matt.sisto Posted May 6, 2009 Author Share Posted May 6, 2009 I've sorted it now, so thanks for your help. Link to comment https://forums.phpfreaks.com/topic/157090-solved-using-results-from-an-sql-query-adding-them-together-to-define-a-variable/#findComment-827489 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.