Get The Specified Column Value


I want to get all value of column user_id in users table. How to?

table users: id(PK),user_id

controller : UsersController.php


class UsersController extends Controller


         public function actionSearch(){

                $command = Yii::app()->db->createCommand('select user_id from users');

                $rows = $command ->queryAll();                   

                foreach($rows as $key=>$val){

                        echo $a = $key."=>".$val;


                print_r($a);//print the results




But I got the result :0=>Array 1=>Array 2=>Array 3=>Array ,all the column’s value are “Array”.And what I want is the user_id,not “Array”.

How to ? Thank you very much!


foreach($rows as $val){

      echo $val -> user_id; // or

      echo $val['user_id'];


Try It!

@ujjwal he is not using AcriveRecord.

as roman said you can use $val[‘user_id’];

Thank you guys! It worked :lol: I think I need know more about php.Thank you again!

You could have also used CDbCommand::queryColumn