Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在smarty模板系统中使用PHP实现数据库求和_Php_Mysql_Sql_Database_Smarty - Fatal编程技术网

在smarty模板系统中使用PHP实现数据库求和

在smarty模板系统中使用PHP实现数据库求和,php,mysql,sql,database,smarty,Php,Mysql,Sql,Database,Smarty,以下是我当前使用的代码: $result = mysql_query(" SELECT SUM(s.amount) FROM tblaffiliatespending s JOIN tblaffiliatesaccounts a ON a.id=s.affaccid JOIN tblhosting h ON h.id = a.relid JOIN tblproducts p ON p.id = h.packageid JOIN tblcl

以下是我当前使用的代码:

   $result = mysql_query("
SELECT SUM(s.amount) 
  FROM tblaffiliatespending s
  JOIN tblaffiliatesaccounts a
    ON a.id=s.affaccid 
  JOIN tblhosting h
    ON h.id = a.relid 
  JOIN tblproducts p
    ON p.id = h.packageid 
  JOIN tblclients c
    ON c.id = h.userid 
 WHERE affiliateid = $affiliateid 
 ORDER 
    BY clearingdate DESC;
    ");
$data = mysql_fetch_array($result);
$pendingcommissions = $data['?????????'];
$this->assign("pendingamount", $pendingcommissions);
我不确定的是报名参加什么?????????在第三行。我尝试了所有这些方法,但没有一种有效:

$pendingcommissions = $data['SUM(tblaffiliatespending.amount)'];
$pendingcommissions = $data['SUM'];
$pendingcommissions = $data['tblaffiliatespending.amount'];
$pendingcommissions = $data['tblaffiliatespending'];
$pendingcommissions = $data['amount'];

您知道需要将其更改为什么吗?

您需要提供金额总和的别名。amount total\u amount因此,当您执行查询并从中获取结果时,您将在结果数组的total\u amount索引中显示金额总和列,您可以通过$data['total\u amount']访问它;,另请注意,如果使用聚合函数而不进行分组,则将导致一行而不是每组

SELECT SUM(s.amount) total_amount
  FROM tblaffiliatespending s
  JOIN tblaffiliatesaccounts a
    ON a.id=s.affaccid 
  JOIN tblhosting h
    ON h.id = a.relid 
  JOIN tblproducts p
    ON p.id = h.packageid 
  JOIN tblclients c
    ON c.id = h.userid 
 WHERE affiliateid = $affiliateid 
 ORDER 
    BY clearingdate DESC

我们真正喜欢的是当整件事都写在一行上时。在这种情况下,其他人会怎么做?使用SUMs.amount作为总计,然后检索$data['total']。此外,mysql_*也不推荐使用