静态查询到动态查询

时间:2019-05-08 01:33:42

标签: php mysql dynamic

我过去经常进行动态查询以导出总输出。但是我只是可以做静态查询。我需要一个适当的查询,然后它才能根据下拉搜索(年,周开始和周结束)导出文件

我使用php和mysql运行该系统。我不能使用相同的查询来显示所有数据。我认为我应该在显示所有数据之前先做一个临时变量,然后才能使用该变量来显示和导出数据

1。获取用户输入并显示的第一页

//对于下拉年份,开始一周和结束一周

if(isset($_POST['search'])){    

$searchq = $_POST['search'];    

$searchq1 = $_POST['search1'];    

$searchq2 = $_POST['search2'];    

//查询以显示所有数据

$sql1 ="SELECT sum(Qtyinspect3k) as '3k', sum(Qtyinspect10k)as '10k', sum(QtyDefect)as 'reject' FROM dbo.OQAVM 
WHERE Year like '%$searchq%' AND WorkWeek BETWEEN '$searchq1' AND '$searchq2' and Day = 'Saturday'  group by WorkWeek order by WorkWeek";

$stmt1 = sqlsrv_query( $conn, $sql1 );

2。导出基于1编码的数据,我只进行静态查询。您可以在年度和工作周看到。我用年和周来获取数据

$setSql ="SELECT LotNo, Package,Machine,BIM,WorkWeek,ProductFamily,QtyDefect,Qtyinspect3k,Qtyinspect10k FROM dbo.OQAVMView WHERE Year like '2017' AND WorkWeek BETWEEN 'ww01' AND 'ww02' ";

$setRec = sqlsrv_query( $conn, $setSql );  

 if( $setRec === false)
{
  die( print_r( sqlsrv_errors(), true) );
}   

$columnHeader = '';  
$columnHeader = "LotNumber" . "\t" ."Package" . "\t" ."Machine" . "\t" . "BIM" . "\t"."WorkWeek" . "\t" ."ProductFamily" ."\t"."QtyDefect" . "\t". "\t"."3K" . "\t"."10K" . "\t"  ;    
$setData = '';  
$rowHeader = '';

while( $rec = sqlsrv_fetch_array( $setRec, SQLSRV_FETCH_ASSOC) ) { 
$rowData = '';  
foreach ($rec as $value) {  
$value = '"' . $value . '"' . "\t";  
$rowData .= $value;  
}  
$setData .= trim($rowData) . "\n";  
}

0 个答案:

没有答案