开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜