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

Community
  • 1
  • 1
user2465936
  • 1,030
  • 4
  • 17
  • 32
  • 1
    manually append and prepend % to value before passing to query as argument. See Example #6 second part http://php.net/manual/en/pdo.prepared-statements.php – Imre L Jun 21 '13 at 08:40

5 Answers5

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...
John Woo
  • 258,903
  • 69
  • 498
  • 492
3

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

i.e:

SELECT * FROM table_name WHERE column_name like '%field_name%';
Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
ejo
  • 446
  • 1
  • 9
  • 23
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

WattoWatto
  • 159
  • 8
  • `?` is a placeholder for a passed in parameter, it won't work in quotes. – Bob Vale Jun 21 '13 at 08:45
  • 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... – WattoWatto Jun 21 '13 at 10:27
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(?,'')
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
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 '%?%'
Tony Stark
  • 2,318
  • 1
  • 22
  • 41