CDbCommand failed to execute the SQL statement

hi

I have got below Error , I am using 2 times queryAll() in my controller action function.

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1065 Query was empty. The SQL statement executed was:

This is my code

           $sql='SELECT DISTINCT( year( date_purchased ) ) AS y FROM orders ORDER BY date_purchased DESC limit 2'; 


	$connection=Yii::app()->db;


	$command=$connection->createCommand($sql);


	$order_years=$command->queryAll();


	Yii::app()->db->setActive(false);


	Yii::app()->db->setActive(true);








            $prods_query_sql = "select * from users ";


	$prods_data=$connection->createCommand($prods_query_sql)->queryAll();

Without last 2 line working fine

any one help me here ?

Thanks

Under the hood Yii uses PDO for manipulating database. This is likely the problem of PDO object but firstly, you can clean up your code.

You created shorthand variable $connection. Why don’t keep using it but use Yii::app()->db again. If you have to refer to Yii::app()->db many times, use the short hand variable will be much faster. In you may see your bug disappear.

The 2nd thing is why do you have to setActive(false) then setActive(true) ? It doesn’t make a lot of sense while doing that mean you close your DB connection and reopen it which is a very costly action.