justinomaha Posted September 2, 2008 Share Posted September 2, 2008 Hi everybody, I'm pretty new to PHP and new to the site. I hope I'm posting this problem in the right forum. I'm trying to calculate a sum from a mysql database and turn it into a variable to use in other areas of the code. Here's what I've been trying so far: $SCustId = $_REQUEST['SCustId']; $paymentquery = "SELECT SUM(Amount) FROM ".PAYMENTSRECEIVED." WHERE SCustID = '".$SCustId."'";"; $paymentresult = mysql_query($paymentquery) or die(mysql_error()); $paymentrow = mysql_fetch_array($paymentresult); $paymenttotal = $paymentrow(SUM(Amount)); When I try to load the page in my browser, I get the following error: "Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING" Does anyone know what I'm doing wrong? Thank you for your help! Link to comment https://forums.phpfreaks.com/topic/122447-turning-a-sum-into-a-variable/ Share on other sites More sharing options...
rarebit Posted September 2, 2008 Share Posted September 2, 2008 change this line to $paymentquery = "SELECT SUM(Amount) FROM ".PAYMENTSRECEIVED." WHERE SCustID = '".$SCustId."'"; ? Link to comment https://forums.phpfreaks.com/topic/122447-turning-a-sum-into-a-variable/#findComment-632251 Share on other sites More sharing options...
pocobueno1388 Posted September 2, 2008 Share Posted September 2, 2008 You need to alias the field, that way you can use it as a variable. So try the following <?php $SCustId = $_REQUEST['SCustId']; $paymentquery = "SELECT SUM(Amount) as total FROM ".PAYMENTSRECEIVED." WHERE SCustID = '".$SCustId."'"; $paymentresult = mysql_query($paymentquery) or die(mysql_error()); $paymentrow = mysql_fetch_array($paymentresult); $paymenttotal = $paymentrow['total']; ?> Link to comment https://forums.phpfreaks.com/topic/122447-turning-a-sum-into-a-variable/#findComment-632256 Share on other sites More sharing options...
rarebit Posted September 2, 2008 Share Posted September 2, 2008 I do it like pocobueno showed, hwever you could have used: $paymenttotal = $paymentrow('SUM(Amount)'); Link to comment https://forums.phpfreaks.com/topic/122447-turning-a-sum-into-a-variable/#findComment-632263 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.