Smarty Where 子句不过滤数据

Smarty Where Clause Not Filtering Data

我正在尝试显示基于 Where Statement 的所有数据,但它显示的是来自 table 的所有数据。下面是我的account.php文件代码

$sql = "SELECT Country,COUNT(*) as count, ROUND(100.0*COUNT(ip)/(SELECT     count(ip)  FROM ip_ptc),2) as percentage  FROM ip_ptc GROUP BY Country


UNION ALL

SELECT 'SUM' ip, COUNT(ip) as sum,'100%'
FROM ip_ptc  WHERE ad_id=" . $db->real_escape_string($input->gc['aid']); 

$result = mysql_query($sql) OR die(mysql_error()); 
while($row = mysql_fetch_assoc($result)) 
{ 
$data[] = $row; # $data is the array created for use in the Smarty template. 
} 
$smarty->assign('data', $data); 
//showing data for addstate

$smarty->assign("file_name", "ptcmaxclicks.tpl");
$smarty->display("account.tpl");
$db->close();
exit();

正在检查 url 它相应地传递了 gc['aid'] 但显示了来自 table 的所有数据而不是根据 gc['aid'] 和我的 account.tpl 文件我有以下代码

{foreach from=$data item=item key=key} 
<tr>
   <td>{$item.Country}</td> <td>{$item.count}</td> <td>{$item.percentage}</td> 
<tr> 
{/foreach} 

我做错了什么?谢谢

您的问题是 WHERE 子句仅适用于第二个 SELECT。您也需要将它添加到第一个 SELECT 即:

$sql = "SELECT Country, COUNT(*) as count, 
            ROUND(100.0*COUNT(ip)/(SELECT count(ip)  FROM ip_ptc),2) as percentage
        FROM ip_ptc 
        WHERE ad_id=" . $db->real_escape_string($input->gc['aid']) . 
       " GROUP BY Country
        UNION ALL
        SELECT 'SUM' ip, COUNT(ip) as sum,'100%'
        FROM ip_ptc
        WHERE ad_id=" . $db->real_escape_string($input->gc['aid']);