Getting Column Name From Queryall()

Hi I am create a project using Yii framework. I am facing some problem to get the list of column name from the result set. Here is the code-




	$sql = 'SELECT 

		  `order`.`order_id`,

		  `order`.`order_date`,

		  `order`.`order_status`,

		  `customer`.`customer_name`,

		  `product`.`product_name`,

		FROM

		  `order`

		  INNER JOIN `customer` ON (`order`.`order_customer_id` = `customer`.`customer_id`)

		  INNER JOIN `product` ON (`order`.`order_product_id` = `product`.`product_id`)

		WHERE

		  `customer`.`customer_id` = 321 ORDER BY order_date DESC';

		

		$connection=Yii::app()->db;

		$command=$connection->createCommand($sql);

		$orderList=$command->queryAll();



How can I get the list column name from the $orderList

Thanks

Well, print_r $orderList and see what it looks like.

http://www.yiiframework.com/doc/guide/1.1/en/database.dao

Try This!




foreach($orderList as $row) {

echo $row -> column; // your column name

}



you can loop thru your $oderList




foreach($orderList as $key=>$value) {

  $column["name_of_your_column"];

}

Thanks for the response, but in the above code $row -> column is invalid because $row does not have any attribute called "column".

Like this maybe?


array_keys($orderList[0])

But won’t work if your query returns nothing. Also if your only goal is to retrieve column names, there should be a better way.

@bennouna it does not returns an array it returns associative array other words hash or dictionary so you cant use integer indexes

@alirz23 what do you mean?


array_keys($orderList[0])

will indeed return an array which values are the names of the columns that are in the original query.

sorry dude my bad i did not see array_keys prefixed

Like this


array(5) {

  [0]=>

  string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "order_id"

  [1]=>

  string(10) "order_date"

  [2]=>

  string(12) "order_status"

  …

}