Getting metadata/column names for empty resultset I have a dynamically generated sql. How can I know what the column names are, even if the database returns an empty result?

$sql = "select t.bar, count(o.foo) as amount
        from craft_table t join craft_otherTable o on t.id = o.id";
$resultset = craft()-&gt;db-&gt;createCommand($sql)-&gt;queryAll(true);


In Java, i can do something like this:

for(int i=0;i&lt;resultset.getMetaData().getColumnCount();i++){
    columnNames.add(resultset.getMetaData().getColumnLabel(i))
}


I am looking for a similar solution in PHP.

I have tried this:

$command = craft()-&gt;db-&gt;createCommand($sql);
$command-&gt;prepare();
var_dump($command-&gt;getPdoStatement()-&gt;getColumnMeta(0));


...but the output is bool(false). I feel like I am missing something.
I was close - I think the problem was the $command-&gt;prepare() did not do anything useful, using $command-&gt;query() executes the query.

This is working for me

    $sql = "select t.foo, count(o.bar) as amount from craft_table t join craft_otherTable o on t.id = o.id where 1=0 group by t.foo;"
    $command = craft()-&gt;db-&gt;createCommand($sql);
    $reader = $command-&gt;query($parameters);
    $statement = $command-&gt;getPdoStatement();
    $result = [];
    $headers = [];
    for($j=0;$j&lt;$statement-&gt;columnCount();$j++){
      $headers[] = $statement-&gt;getColumnMeta($j)['name'];
    }
    $result['headers'] = $headers;
    $result['rows'] = $reader-&gt;readAll();

    // $result['headers'] is now: ['foo', 'amount']
    // $result['rows'] is now: []