Jump to content

Turning a Sum into a Variable


justinomaha

Recommended Posts

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

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

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.