Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine PHP prepared statments with LIKE

Tags:

php

sql-like

Anyone know how to combine PHP prepared statements with LIKE? i.e.

"SELECT * FROM table WHERE name LIKE %?%";

like image 973
user29772 Avatar asked Mar 19 '09 05:03

user29772


1 Answers

The % signs need to go in the variable that you assign to the parameter, instead of in the query.

I don't know if you're using mysqli or PDO, but with PDO it would be something like:

$st = $db->prepare("SELECT * FROM table WHERE name LIKE ?");
$st->execute(array('%'.$test_string.'%'));

For mysqli user the following.

$test_string = '%' . $test_string . '%';
$st->bind_param('s', $test_string);
$st->execute();
like image 60
Chad Birch Avatar answered Sep 19 '22 03:09

Chad Birch