2

I have a line like this:

 $line= Yii::app()->db->createCommand()->select('line_no')->from('tblvehicleimage') ->where('serial_no=:serial_no', array(':serial_no'=>$model->serial_no)) ->queryScalar();

how to put all the $line data in an array? Is there any way i can use 'mysql_fetch_array'?

2 Answers 2

2

queryScalar means only one record will be retrieved.

You want to use serial_no IN (1, 2, 3) for example, and queryAll() instead of queryScalar().

$serial_nos = array(1, 2, 3);
$serial_nos = implode($serial_nos);

$line = Yii::app()->db->createCommand()
        ->select('line_no')
        ->from('tblvehicleimage')
        ->where('serial_no IN ('.$serial_nos.')')
        ->queryAll();

Once the query is successful, $line will contain an array, you then simply have to loop through the results:

foreach ($line as $key=>$item) {
    // do something with each $item
}
Sign up to request clarification or add additional context in comments.

3 Comments

o ok, so queryScalar was the problem..; thnx a lot.
using findAll() giving error: CDbCommand and its behaviors do not have a method or closure named "findAll". Maybe it wil be queryAll()?
it will give you an error obviously because querybuilder method does not support find().
2

Since you want to select only line_no from the table, you can also use queryColumn()

$line = Yii::app()->db->createCommand()
        ->select('line_no')
        ->from('tblvehicleimage')
        ->where('serial_no IN ('.$serial_nos.')')
        ->queryColumn();

If you want one-dimensional array like

array(1,2,3)

as your result then you can use queryColumn()
But if you want

array(array(1),array(2),array(3))

as your answer then you can use queryAll().

Choice is yours :)

Comments

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.