3

Based on information here MySQL query String contains trying to create pdo query with ?

Experimented with following

SELECT * FROM Table WHERE Column LIKE %?%

SELECT * FROM Table WHERE Column LIKE ?%

SELECT * FROM Table WHERE Column LIKE %?

Nothing works. Get error

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%...

Tried with SELECT * FROM Table WHERE Column LIKE ? but this is not for contains

Aim is to get query SELECT * FROM Table WHERE Column contains ?

What is is correct pdo contains statement for positional placeholders (?)?

1

5 Answers 5

7

try this by concatenating the value in the string via PHP,

$value = "valueHere";
$passThis = "%" . $value . "%";
// other pdo codes...
$stmt = $dbh->prepare("SELECT * FROM Table WHERE Column LIKE ?");
$stmt->bindParam(1, $passThis);
// other pdo codes...
Sign up to request clarification or add additional context in comments.

Comments

3

after like add quotes. eg:- like '%?%'

i.e:

SELECT * FROM table_name WHERE column_name like '%field_name%';

1 Comment

? is a placeholder for a passed in parameter, it won't work in quotes.
1

I think wildcard stament should be within single quotes, like;

SELECT * FROM Table WHERE Column LIKE '%?%';

This returns any record which contains the string given anywhere within the particular column field

Column data which starts with 'ber', example

SELECT * FROM Table WHERE Column LIKE 'ber%';

Hope this helps

2 Comments

? is a placeholder for a passed in parameter, it won't work in quotes.
i expected pdo to be flexible, it seems pdo would tie your hands up for each query. well the price that you pay to be constructive i guess...
1

Either put the % characters before and after your parameter before you pass it into the query or

SELECT * FROM Table WHERE Column LIKE '%' + ? + '%'

SELECT * FROM Table WHERE Column LIKE ? + '%'

SELECT * FROM Table WHERE Column LIKE '%' + ?

Although this will fail if ? is null because the concatenate will yield null. you could use coalesce

SELECT * FROM Table WHERE Column LIKE '%' + Coalesce(?,'') + '%'

SELECT * FROM Table WHERE Column LIKE Coalesce(?,'') + '%'

SELECT * FROM Table WHERE Column LIKE '%' + Coalesce(?,'')

Comments

0

If you would like to use prepared statements then take a look at http://php.net/manual/de/pdo.prepared-statements.php.

SELECT * FROM REGISTRY where name LIKE '%?%'

1 Comment

php.net/manual/en/pdo.prepared-statements.php Example #6 Invalid use of placeholder

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.