MySQL query that looks for substring in column?
I'm using this query to look for User results. The problem is that it only finds exact matches. How do I make it check for string as substring in column User?
$result2 =开发者_如何学Go mysql_query("SELECT * FROM `Users` WHERE MATCH (`User`) AGAINST ('$string') LIMIT 5");
Thanks
$result2 = mysql_query("SELECT * FROM `Users` WHERE `User` LIKE '%$string%' LIMIT 5");
Use the LIKE
operator, that's what it's there for.
Also, I hope you are escaping the $string
variable earlier in the code with mysql_real_escape_string
to protect against SQL Injection.
$result2 = mysql_query("SELECT * FROM `Users` WHERE `User`LIKE '%".$string."%' LIMIT 5");
Change your query to use the LIKE
keyword and the %
thingy-mabobber:
SELECT * FROM `Users` WHERE `User` LIKE '%$string%' LIMIT 5
It should be something like:
SELECT * FROM pet WHERE name LIKE '%fy';
You can read more here.
I think you want to use the LIKE operator.
Something along the lines of:
$result = mysql_query("SELECT * FROM `Users` WHERE `User` LIKE '%$string%' LIMIT 5");
http://www.w3schools.com/sql/sql_like.asp
精彩评论