Manir
(Monirit)
1
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
dniznick
(Dniznick)
2
Well, print_r $orderList and see what it looks like.
_uJJwAL
(_uJJwAL_)
3
http://www.yiiframework.com/doc/guide/1.1/en/database.dao
Try This!
foreach($orderList as $row) {
echo $row -> column; // your column name
}
alirz23
(Ali Raza)
4
you can loop thru your $oderList
foreach($orderList as $key=>$value) {
$column["name_of_your_column"];
}
Manir
(Monirit)
5
Thanks for the response, but in the above code $row -> column is invalid because $row does not have any attribute called "column".
abennouna
(Abennouna)
6
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.
alirz23
(Ali Raza)
7
@bennouna it does not returns an array it returns associative array other words hash or dictionary so you cant use integer indexes
abennouna
(Abennouna)
8
@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.
alirz23
(Ali Raza)
9
sorry dude my bad i did not see array_keys prefixed
abennouna
(Abennouna)
10
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"
…
}