简体   繁体   中英

SELECT * FROM Table WHERE Column LIKE %?%

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 ( ? )?

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...

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

ie:

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

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

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(?,'')

如果您想使用预准备语句,请查看http://php.net/manual/de/pdo.prepared-statements.php

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM