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.

5
  • Is that a quote from someone important? Commented Oct 11, 2018 at 8:01
  • when i write <?php echo "$us"; ?> display is (array) I would like to insert a single number like 3 => fill in the default_number_of_devices ricord because it uses where id 1, and only appears one digit Commented Oct 11, 2018 at 8:05
  • First you should understand that you can't echo an array. Commented Oct 11, 2018 at 8:07
  • the above query is to define select default_number_of_devices from user where id = 1; if in sql only appears one number like number 3 but when I roll over $us just write the array instead of number 3 Commented Oct 11, 2018 at 8:20
  • It you want to array instead of single column value, then you should try queryAll(). And please ask you questions in proper way with exact output and expected output. Commented Oct 11, 2018 at 8:54

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

it's use ->queryScalar(); and not queryRow()
@kagami there are three ways to fetch the data. First one is queryScalar() which returns just the single value from query, which you wanted here. Second one is queryRow() which returns a single row from query as array. Last, there is queryAll() which returns one array with all rows and columns. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.