We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to resolve: Invalid parameter number: number of bound variables does not match number of tokens

I have this function:

public function updateNumberOfProduct($productId, $type='+') {
    if ( $type == '+' )
        $number = 1;
    else
        $number = -1;

    $di = \Phalcon\DI::getDefault();
    $productPHQL = new Query(
        "UPDATE \App\Model\Products SET \App\Model\Products.number = \App\Model\Products.number + :number: 
        WHERE \App\Model\Products.product_id = :product_id: AND \App\Model\Products.number > 0",
        $di
    );

    $result = $productPHQL->execute(
        [
            "number" => $number,
            "product_id" => $productId
        ]
    );
}

When executed, I saw this error: Invalid parameter number: number of bound variables does not match number of tokens

What did I do wrong?

Thanks.

edited Oct '16

Why you are not using modelsManager ? Don't create query on your own :) use:

$modelsManager = $di->get('modelsManager');
$productPHQL = $this->modelsManager->createQuery(
 "UPDATE \App\Model\Products SET \App\Model\Products.number = \App\Model\Products.number + :number: 
        WHERE \App\Model\Products.product_id = :product_id: AND \App\Model\Products.number > 0"
        );

Also use model namespace aliases. FQN looks kind of bad in queries.



6.0k
edited Oct '16

Why don't I create query on my own? I have read the document (https://docs.phalcon.io/en/latest/reference/phql.html#creating-phql-queries) and haven't seen any reference about that.

In my opinion, they are the same.

About use Model namespace aliases, I have read https://docs.phalcon.io/en/latest/reference/namespaces.html#models-in-namespaces but don't know how to use the alias in UPDATE statement, in SELECT statement, alias is OK.

Anw, when I use modelManager, I continue to see the error Invalid parameter number: number of bound variables does not match number of tokens. If my statement is:

"UPDATE \App\Model\Products SET \App\Model\Products.number = :number: 
        WHERE \App\Model\Products.product_id = :product_id: AND \App\Model\Products.number > 0"

(Change \App\Model\Products.number = \App\Model\Products.number + :number: to \App\Model\Products.number = :number:)

It's OK.



6.0k

@Jurigag: Do you have any idea about my issue?