2011-08-16 16 views
1

我正在使用此查询来查找用户结果。问题是它只能找到完全匹配。如何使它在列User中检查字符串作为子字符串?在查找列中的子串的MySQL查询?

$result2 = mysql_query("SELECT * FROM `Users` WHERE MATCH (`User`) AGAINST ('$string') LIMIT 5"); 

感谢

回答

4
$result2 = mysql_query("SELECT * FROM `Users` WHERE `User` LIKE '%$string%' LIMIT 5"); 

使用LIKE运营商,这就是它的存在了。

另外,我希望你在mysql_real_escape_string的代码中早先转义$string变量,以防止SQL Injection

1
$result2 = mysql_query("SELECT * FROM `Users` WHERE `User`LIKE '%".$string."%' LIMIT 5"); 
+0

字符串连接是昂贵的。 – Shef

1

更改您的查询使用LIKE关键字和%啄-mabobber:

SELECT * FROM `Users` WHERE `User` LIKE '%$string%' LIMIT 5 
0

它应该是这样的:

SELECT * FROM pet WHERE name LIKE '%fy'; 

你可以阅读更多here