Search code examples
phpyiiyii1.x

How to get an array value from the query in Yii 1.1


$us= Yii::app()->db->createCommand()
    ->select('default_number_of_devices')
    ->from('user')
    ->where('id=1')
    ->queryRow();
echo "$us";

I should have got one value instead of array type because id is unique. But $us appears to be an array instead of single number.


Solution

  • You should use queryScalar() if you want to get single value from single column:

    $us = Yii::app()->db->createCommand()
        ->select('default_number_of_devices')
        ->from('user')
        ->where('id=1')
        ->queryScalar();
    echo $us;
    

    queryRow() returns first row from query. And since row usually contains multiple columns, array is expected format (each element of array contains value of single column).