tags:

views:

542

answers:

1

Hi all,

Quick question: How do I mysqli_escape_string a variable enclosed in a like clause?

"SELECT * FROM table WHERE name LIKE '%". %s . "%'"

or

"SELECT * FROM table WHERE name like '%"."%s"."%'"

don't work.

Thanks!

+2  A: 
$value = mysql_real_escape_string($_POST["terms"]);
$query = "SELECT * FROM table WHERE name LIKE '%".$value."%'";

Or you could acheive this with sprintf like this:

$query = sprintf("SELECT * FROM table WHERE name LIKE '%s'", "%".$value."%");
Jonathan Sampson
Hm let me check that out real quick -- I've been accustomed to sprintf("SELECT ", mysqli_escape_string($link, $var))
Michael
I fixed it to work using your method, but was unable to make it work with sprintf
Michael
@Michael - Try the updated-example for sprintf.
Jonathan Sampson