使用ODBC PDO从查询中获取列名称/别名

时间:2015-06-15 14:15:35

标签: php pdo

我正在为我的团队构建一个自定义查询编写器,他们可以简单地放入查询,然后将其导出到csv文件。它使用ODBC PDO驱动程序。如何提取列名称/别名?查询通常会包含以下内容:

$query = "SELECT
jcst.Loc_No,
jcst.Emp_No,
round(Sum(jcst.Amt_1), 2) AS Gross_Wages,
round(Sum(jcst.Amt_12),2) as Local_Tax,
round(sum(jcst.Amt_3+jcst.Amt_2),2) As Fica,
round(sum(jcst.Amt_4),2) as FUTA,
round(Sum(jcst.Amt_5),2) as SUTA,
round(sum(jcst.Amt_10),2) as Admin_Fee,
round(Sum(jcst.Amt_13),2) as Other_Costs
FROM Master";

我需要Loc_No,Emp_No,Gross_Wages,Local_Tax,FICA,SUTA,Admin_Fee,Other_Costs作为标题列信息。如何根据查询获取列/别名?

由于

2 个答案:

答案 0 :(得分:0)

$sth->setFetchMode(PDO::FETCH_ASSOC);

foreach ($sth->fetch() as $key => $value) {
    $array[] = $key;
}

var_dump($array);

对于那些想知道如何做的人来说!

答案 1 :(得分:-1)

you may try if you are expecting the display as below

$result = mysqli_fetch_array(....);
...
foreach ($result as $res)
{
echo $res['Loc_No']."<br \>";
echo $res['Emp_No']."<br \>";
echo $res['Gross_Wages']."<br \>";
echo $res['Local_Tax']."<br \>";
echo $res['FICA']."<br \>";
echo $res['SUTA']."<br \>";
echo $res['Admin_Fee']."<br \>";
echo $res['Other_Costs']."<br \>";
}
....