使两个SQL查询同时工作

时间:2013-02-01 12:02:03

标签: php sql zip

我在一个php文件中有两个查询但只执行第二个查询。但是第一个查询没有被执行。我能做错什么?这是我的代码。

<?php include('connections/pgconn.php'); ?>
<?php


function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}

// filename for download
$filename = "eastafrica_revenue_monthly" . date('Ymd') . ".xls";

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");

$flag = false;
$result = pg_query("SELECT to_char (a.CALLDATE,'yyyymm') as month,min(a.calldate) as   start_time,max(a.calldate) as end_time,
   sum(a.alltaxcost::integer) AS revenue,
     COUNT (DISTINCT a.IDENTIFIANT) AS distinct_callers,
a.zoneiddest as country_code,b.country
FROM cdr_data a,COUNTRY_CODES b
WHERE  a.CALLSUBCLASS = '002'
     AND a.CALLCLASS = '008'
and a.zoneiddest::integer > 0
AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
('77', '78', '75', '70', '71', '41', '31', '39', '76','79')
and  substr(a.zoneiddest,1,3)  in ('254','255','211','257','250')
and trim(a.zoneiddest)  = trim(b.country_code)
GROUP BY to_char (a.CALLDATE,'yyyymm') ,a.zoneiddest,b.country
ORDER BY 1");

while(false !== ($row = pg_fetch_assoc($result))) {
if(!$flag) {
  // display field/column names as first row
  echo implode("\t", array_keys($row)) . "\r\n";
  $flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\r\n";
}


?>

<?php
function cleanData2(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}

// filename for download
$filename = "international_revenue_daily" . date('Ymd') . ".xls";

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");

$flag = false;
$result = pg_query("SELECT to_char (a.CALLDATE,'yyyymmdd') as transdate,
    sum(a.alltaxcost::integer) AS revenue,
     COUNT (DISTINCT a.IDENTIFIANT) AS distinct_callers,
a.zoneiddest as country_code,b.country
FROM cdr_data a,  COUNTRY_CODES b
WHERE  a.CALLSUBCLASS = '002'
     AND a.CALLCLASS = '008'
and a.zoneiddest::integer > 0
AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
('77', '78', '75', '70', '71', '41', '31', '39', '76','79')
and not substr(a.zoneiddest,1,3) in ('254','255','211','257','250','256')
and trim(a.zoneiddest)  = trim(b.country_code)
GROUP BY to_char (a.CALLDATE,'yyyymmdd') ,a.zoneiddest,b.country
ORDER BY 1") ;

while(false !== ($row = pg_fetch_assoc($result))) {
if(!$flag) {
  // display field/column names as first row
  echo implode("\t", array_keys($row)) . "\r\n";
  $flag = true;
}
array_walk($row, 'cleanData2');
echo implode("\t", array_values($row)) . "\r\n";
}
exit;
?>

0 个答案:

没有答案