[solved] Invalid parameter number

i made a function like this :


public function actionAdmin()

	{

		$date = explode("a",$_GET['date']);

		$media = $_GET['media'];

		$adv = $_GET['adv'];

		$prod = $_GET['prod'];

		$group = $_GET['group'];

		

		$criteria = new CDbCriteria;

		$criteria->addCondition('start_periode >= :to AND stop_periode <= :from');

		$criteria->params=array(

			':to'=>$date[0]." 00:00:00",

			':from'=>$date[1]." 23:59:59",

		);

			);

it’s work but when i put another code it made error

the code i add is


if($media != null)

			$criteria->addCondition('id_media_seller = :prm');

			$criteria->params=array(

				':prm'=>$media,

did i miss something?

the error was CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens. The SQL statement executed was: SELECT COUNT(*) FROM tbl_batch t WHERE (start_periode >= :to AND stop_periode <= :from) AND (id_media_seller = :prm)

You missed curvy brackets:




if($media != null){

                        $criteria->addCondition('id_media_seller = :prm');

                        $criteria->params=array(

                                ':prm'=>$media,);


}



still not working

if you want to get the value. try $_GET[‘Model’][‘attribute’] is same with $model->attribute if we get from database

. i hope this can help your problem…

thanks but it still error

In the above, the 2nd “$criteria->params=…” will overwrite the 1st one, and ‘:to’ and ‘:from’ will be lost and only ‘:prm’ will remain in $criteria->params.

So, try the following:




public function actionAdmin()

	{

		...

		if($media != null){

			$criteria->addCondition('id_media_seller = :prm');

-			$criteria->params=array(

-				':prm'=>$media,

-			);

+			$criteria->params[':prm']=$media;

		}

		...



[EDIT]

Sorry.

The code should be:




public function actionAdmin()

	{

		...

		if($media != null){

			$criteria->addCondition('id_media_seller = :prm');

			$criteria->params += array(':prm'=>$media);

		}

		...



sorry, its still not work

i use this code and it’s working now but just for the $media not null, if its null its error


$criteria->addCondition('date >= :to AND date <= :from');

				if($media != null)

				$criteria->addCondition('id_media_seller = :prm');

			$criteria->params=array(

				':to'=>$date[0],

				':from'=>$date[1],

				':prm'=>$media,

			);

i try to add this code


if($media != null)

                                ':prm'=>$media,

in $criteria->params=array, but this not working. how to add the if condition in params?

thanks

this simple code to make conditions with params

so i make condition where the id from downcatid is $_GET[‘id’]

and i use dataProvider. i hope this help you




public function actionList()

	{

		$criteria= new CDbCriteria(array(

				'condition'=>'active=0 AND downcatid=:downcatid',

				'params'=>array(':downcatid'=>$_GET['id']),

			));

		

		$dataProvider=new CActiveDataProvider('Down',array(

			'criteria'=>$criteria,

		));

		$this->render('list',array(

			'dataProvider'=>$dataProvider,

		));

	}

	






thanks rin for the help, it solved now