如何检索在数据库中动态创建的列

时间:2015-04-07 12:50:41

标签: php mysql dynamic output

我在数据库中创建表,其中根据不同表中存在的日期值动态创建月份字段。代码是:

//Get avaialable moths in the table
$sql ="Select Distinct MONTHNAME(STR_TO_DATE(MONTH(test_date), '%m')) monthname from emp_csv order by MONTH(test_date) ASC ";
$i=0;
foreach($_pdo->query($sql) as $row)    {
     $reportnoarr[$row["monthname"]]=$i;
     $monthconstruct[$i++]=$row["monthname"];

}
$implodedmonthname=implode($monthconstruct," varchar(30),")." varchar(30),totalreport varchar(30) ";


//Creating dynamic table 
$sql ="DROP  TABLE t_month";
$stat = $_pdo->query($sql); 
$sql ="CREATE  TABLE t_month ( region varchar(25),engineer varchar(50),$implodedmonthname)";
$stat = $_pdo->query($sql); 

   for($kj=0;$kj<12;$kj++){
                   $reportnumberarray[$kj]=0;
           }
//inserting data to the newly ceated table
$sql ="Select territory,web_rep_no, test_performed_by,MONTHNAME(STR_TO_DATE(MONTH(test_date), '%m')) monthname  from emp_csv order by test_performed_by, monthname,web_rep_no ";



$preveng="";
$totalrep=0;
$territoryname="";
$prevreport="";
$countrep=0;
$prevmonth="";
foreach($_pdo->query($sql) as $row)   {
$nexteng= $row["test_performed_by"];
$nextreport=$row["web_rep_no"];
$nextmonth=$row["monthname"];
if($preveng!=$nexteng && $preveng!=""){
$totalrep=$totalrep+$countrep;
for($mj=0;$mj<count( $monthconstruct);$mj++){
$reportar[$mj]=$reportnumberarray[ $reportnoarr[$monthconstruct[$mj]]];
}
$imploded="'".implode($reportar,"','")."'";
$sql = "INSERT INTO t_month VALUES ('$territoryname','$preveng',$imploded,'$totalrep')"; 

$stat = $_pdo->query($sql); 
$totalrep=0;
$countrep=0;
$prevmonth="";
$prevreport="";
   for($kj=0;$kj<12;$kj++){
                   $reportnumberarray[$kj]=0;
           }
}
if($prevmonth!=$nextmonth){
$totalrep=$totalrep+$countrep;
$countrep=0;

}
if($prevreport!=$nextreport)
$countrep++;



$reportnumberarray[$reportnoarr[$row["monthname"]]]=$countrep;

$preveng=$nexteng;
$territoryname=$row["territory"];
$prevreport=$nextreport;
$prevmonth=$nextmonth;
}

现在我想在输出时动态显示动态创建的所有字段,而不使用select * from table函数。谁可以帮我这个事?

0 个答案:

没有答案