Php 在Smarty模板中运行的MySQL查询

Php 在Smarty模板中运行的MySQL查询,php,mysql,smarty,Php,Mysql,Smarty,我试图在smarty模板文件中使用MySQL查询来显示代销商付款的总金额。我相信下面发布的代码是“正确的”,但我只是遗漏了其中的一部分 {php} $result = select_query("tblaffiliatespending", "SUM(tblaffiliatespending.amount)", array("affiliateid" => $id), "clearingdate", "DESC", "", "tblaffiliatesaccounts ON tblaffi

我试图在smarty模板文件中使用MySQL查询来显示代销商付款的总金额。我相信下面发布的代码是“正确的”,但我只是遗漏了其中的一部分

{php}
$result = select_query("tblaffiliatespending", "SUM(tblaffiliatespending.amount)", array("affiliateid" => $id), "clearingdate", "DESC", "", "tblaffiliatesaccounts ON tblaffiliatesaccounts.id=tblaffiliatespending.affaccid INNER JOIN tblhosting ON tblhosting.id=tblaffiliatesaccounts.relid INNER JOIN tblproducts ON tblproducts.id=tblhosting.packageid INNER JOIN tblclients ON tblclients.id=tblhosting.userid");
$data = mysql_fetch_array($result);
$pendingcommissions = $data[pendingcommissions];
$this->assign("pendingamount", $pendingcommissions);
{/php}
然后我在下面展示了这个变量在页面上:

{$pendingamount}

每当我加载页面时,它都会显示一个空白点,数字应该在哪里…

这是一种奇怪的使用smarty的方式。不能使用$this(它引用模板外的smarty对象)分配值。通常,调用smarty的php应通过smarty对象将值分配给smarty模板。不要将应用程序逻辑放入smarty模板中。

仅供参考,您可以在smarty中分配简单的变量。但是上面的内容仍然适用。

您应该切换到mysql,或者因为mysql从PHP 5.5开始就不推荐使用

我假设select_查询是您自己编写的函数,并且假设您传递了正确的参数

$pendingcommissions=$data[pendingcommissions];
应该给您一个错误。它应该是
$pendingcommissions=$data['pendingcommissions'];


如果之后没有得到结果,请尝试添加一些调试输出,以检查您使用的变量。

官方不鼓励将php嵌入smarty模板中,这被认为是错误的做法,并且在版本3中已被弃用。如果您正在学习smarty,您确实应该尝试在不使用{php}的情况下执行相同的操作(即,在php文件中执行查询并将结果作为变量发送给smarty)。这样可以避免将来出现许多问题。