M
M
MdaUZH2016-03-13 09:38:29
PHP
MdaUZH, 2016-03-13 09:38:29

Prepare statement for like?

Hello everyone, there was a problem with preparing an expression for SQL LIKE ..
There is a query like:

prepare...('SELECT * FROM table WHERE field LIKE '%:like%' ...);
и тут же делаю такой бинд:
$bind['like'] = $myVar

а массив $bind передаю в $pdo->execute($bind)

But it doesn’t find anything, although there are lines, I do it without Prepare Statement: it finds everything. In what an essence - How to prepare correctly expression for LIKE SQL?
$pdo->prepare(' SELECT ... LIKE '%text%'..);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolay, 2016-03-14
@MdaUZH

This is a feature of prepared expressions. It should be like this:

prepare...('SELECT * FROM table WHERE field LIKE :like ...');
$bind['like'] = '%' . $myVar . '%';

R
res2001, 2016-03-13
@res2001

I didn’t work closely with PostGRE, but from experience with other DBMS, it should somehow turn out like this: field
LIKE '%'+:like+'%'
treat them like variables, not constants.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question