I'm new to yii and trying to fetch and associative array with queryall() method. After reading up on it it seems like it should be the default but not working by me. Can abyone help?
Greatly appreciated.
Page 1 of 1
fetchassociative How to fetch associative array in yii
#2
Posted 08 February 2012 - 02:24 PM
Here is some code tested on a real project of mine:
This returns what a mysql_fetch_assoc would return on a mysql db.
To see the returned value so that you can parse it try a print_r.
Here's my result:
I can now parse it very easily and print the name attribute as it is indeed an associative array:
$sql = "SELECT * FROM common_attributes_locale WHERE category_id=:category_id";
$categoryId = 1;
$command = Yii::app()->db->createCommand($sql);
$command->bindParam("category_id", $categoryId, PDO::PARAM_INT);
$rows = $command->queryAll();
This returns what a mysql_fetch_assoc would return on a mysql db.
To see the returned value so that you can parse it try a print_r.
Here's my result:
print_r($rows);
Array (
[0] => Array ( [category_id] => 1 [culture_id] => 1 [name] => Company [description] => 'A company')
[1] => Array ( [category_id] => 1 [culture_id] => 2 [name] => Entreprise [description] => 'Une Entreprise' )
)
I can now parse it very easily and print the name attribute as it is indeed an associative array:
foreach($rows as $currentRow) {
echo $currentRow['name'];
}
#3
Posted 09 February 2012 - 02:06 PM
Thankx. Exactly what I needed explained.
Much clearer than anything i could've read.
Much clearer than anything i could've read.
Share this topic:
Page 1 of 1

Help











