3

I have a search page on my website where you can type in the username of a person to bring up their profile although it only works when you type in their full username. I want to check if a rows username column contains a sequence of characters that the user types in so that when they type in: 'Mr' it will bring up the result 'MrDare360' and so on.

This is the code I have at the moment: http://pastebin.com/68dgLZf8

What would be the easiest or best way of doing this?

4

2 回答 2

10

Use LIKE. Contrived example query:

SELECT name FROM users WHERE name LIKE 'Mr%';

The % character is a wildcard. The above example would match a string starting with Mr (as your question states). You could also do %Mr% to find Mr in the middle of a string or %Mr to find Mr at the end of a string.

Docs here

于 2013-01-17T22:24:53.847 回答
0

Try:

$query = $sqli_con->query("SELECT id, username, avatar, first_name, last_name, country, gender, user_level FROM members WHERE username LIKE '%$search_query%'");
于 2013-01-17T22:24:35.620 回答