Fetch Fields For Db Query

Good day,

I am writing a report generator whereby SQL queries are stored in a database and used to generate excel reports. I am using Yii as my frontend as well as Yii’s database classes for execution of the SQL queries.

The issue I am having is retrieving the column names from the SQL query to be run.

Currently I get around this as follows:




    public function getColumnNames() {

        $data_array = array_keys($this->report_data[0]);

        $this->header_cols = $data_array;

    }



Which works, however if the query returns zero results, I get an "Undefined offset: 0 " error.

So my question is, does yii provide a function to return the column names alone from a SQL query, similar to the way in which PHP’s mysqli_result::fetch_fields does?

You can use something like this if you’re using a MySQL database:




		$reader = $command->query();

		$pdoStatement = $command->getPdoStatement();

		$headers = array();


		for ($i = 0; $i < $reader->columnCount; $i++)

		{

			$meta = $pdoStatement->getColumnMeta($i);

			$headers[] = $meta['name'];

		}


		$reader->close();