smarty where子句不过滤数据

crcmnpdw  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(411)

我正在尝试显示基于 Where Statement 但它显示了表中的所有数据。下面是我的 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'] 但显示表中的所有数据不符合 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}

我怎么了?谢谢

pkmbmrz7

pkmbmrz71#

你的问题是 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']);

相关问题