We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.'%',  
                                    ],
                            ]
                            );