如果不返回任何数据,我想获取查询中选择的列
$sql = $this->em->getConnection()->prepare('
SELECT
DATE_FORMAT(cus.period, "%Y") as ANNEE,
DATE_FORMAT(cus.period, "%m") as MOIS,
c.name AS PAYS,
co.id AS ORIGINE_ID,
co.name AS ORIGINE,
FROM customs AS cus
WHERE cp.product in (503)
GROUP BY ANNEE, MOIS, PRODUCT, co.id , c.id
ORDER BY ANNEE, MOIS, CAMPAGNE, PRODUCT, co.id, c.name;'
);
$sql->execute();
$result = $sql->fetchAll();
我没有数据,结果是一个空数组,但是我想要万一没有数据结果应该是一个像这样的数组
$result = [
'ANNEE', 'MOIS', 'PAYS', 'ORIGINE_ID', 'ORIGINE'
]
答案 0 :(得分:1)
getConnection()
返回包装的PDO,这意味着我们可以使用getColumnMeta()
获取列名:
$stmt = $sql->execute();
$columnNames = array();
foreach(range(0, $stmt->columnCount() - 1) as $index) {
$columnNames[] = $stmt->getColumnMeta($index)['name'];
}
然后可以将 $columnNames
添加到您的$result