Invalid Parameter Number When Quering Db

I have problem using CDbCriteria, this is my code:


        $age = 30;

        $languages = array("en","fr");

        $criteria = new CDbCriteria;

        $criteria->select="name, surname";



        $criteria->params = array(':age' => $age );

        $post = Customers::model()->findAll($criteria);

It results in :

Invalid parameter number: number of bound variables does not match number of tokens

I tried to add the parameters in multiple ways with no luck. What am I doing wrong? Thanks.

I’m not certain whether this will work, but try appending to the params array instead of replacing it:

$criteria->params[':age'] = $age;

Thanks, it works!

Why is that, I had the same problem. I couldn’t bind parameters just by using this

 $criteria->params = array(':age' => $age );

Because you’re overwriting parameters that other CDbCriteria methods have set. In the example from the first post, this:


will have set a parameter behind the scenes. If you replace $criteria->params instead of adding to it, the query that’s generated will have a placeholder with no matching parameter.