Yii Framework Forum: fetchassociative - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

fetchassociative How to fetch associative array in yii Rate Topic: -----

#1 User is offline   Diane 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 08-February 12

Posted 08 February 2012 - 11:30 AM

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.
0

#2 User is offline   Vassilis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 30-June 11

Posted 08 February 2012 - 02:24 PM

Here is some code tested on a real project of mine:

 $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'];
}

0

#3 User is offline   Diane 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 08-February 12

Posted 09 February 2012 - 02:06 PM

Thankx. Exactly what I needed explained.
Much clearer than anything i could've read.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users