1

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 = mysql_query("SELECT * FROM `Users` WHERE MATCH (`User`) AGAINST ('$string') LIMIT 5");

Thanks

5 Answers 5

4
$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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

String concatenation is costly.
1

Change your query to use the LIKE keyword and the % thingy-mabobber:

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

Comments

0

It should be something like:

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

You can read more here.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.