1

Is it possible to execute a query and just get the column names of the returned result set. I need the column names since the query is dynamic and I don't know the names of the columns.

I will use these column names for sorting when executing the query again.

You could refer to my Previous question to get the idea why I need it.

Thanks.

4

1 回答 1

1

根据正在使用的 PDO 驱动程序,一旦执行了语句,您就可以从PDOStatement::getColumnMeta获取列名。

这是在Yii 1.1中可以完成的一种方法:

$command = Yii::app()->{db}
  ->createCommand('SELECT "." `Stop!`, current_time `Hammer Time`');
$reader = $command->query();

$sth = $command->getPdoStatement();
for ($i = 0; $i < $sth->columnCount(); $i++) {
  $col = $sth->getColumnMeta($i);
  print $col['name'].' ';
}
于 2019-12-25T14:38:49.843 回答