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

Correct way of binding in conditions with variables

When I try make a consult with a codition like and a variable bind is returning:

Exception: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

I try put :name: and anothers way of bind and I get the same error or OP CODE error 58

            $item = Item::find(
                            [
                                    'conditions' => "name LIKE '%?0%' ",
                                    'columns' => 'name',
                                    'limit' => 10,
                                    'bind' => [
                                            $busca
                                    ],
                            ]
                            );

If I put literally works:


                    $item = Item::find(
                            [
                                    'conditions' => "name LIKE '%Os%' ",
                                    'columns' => 'name',
                                    'limit' => 10,
                            ]
                            );
$item = Item::find(
                            [
                                    'conditions' => "name LIKE :name: ",
                                    'columns' => 'name',
                                    'limit' => 10,
                                    'bind' => [
                                            'name'=>'%Os%'
                                    ],
                            ]
                            );


3.8k

but I wanna pass a variable in the where-like, I try this too:


$item = Item::find(
                            [
                                    'conditions' => "name LIKE ?1  ",
                                    'columns' => 'name',
                                    'limit' => 5,
                                    'bind' => [
                                        1 => "''%'.$search.'%''",  
                                    ],
                            ]
                            );


12.8k
Accepted
answer
edited Sep '17

just dont set the quotes string, the binding is there to do it :

$item = Item::find(
                            [
                                    'conditions' => "name LIKE ?1  ",
                                    'columns' => 'name',
                                    'limit' => 5,
                                    'bind' => [
                                        1 => '%'.$search.'%',  
                                    ],
                            ]
                            );