54

How can I select a row in my MySQL DB where the value of a column contains 'XcodeDev' for example?

I tried:

SELECT * FROM Accounts WHERE Username LIKE '$query'

But it only selects a row were the Username value is exactly the same to the query.

What can I do to achieve what I want?

0

4 Answers 4

102

Use the % wildcard, which matches any number of characters.

SELECT * FROM Accounts WHERE Username LIKE '%query%'
Sign up to request clarification or add additional context in comments.

2 Comments

I think you are missing the doller sign
very useful +1.
18

This should work:

SELECT * FROM Accounts WHERE Username LIKE '%$query%'

1 Comment

This is used to denote variable names in PHP.
7

My suggestion would be

$value = $_POST["myfield"];

$Query = Database::Prepare("SELECT * FROM TABLE WHERE MYFIELD LIKE ?");
$Query->Execute(array("%".$value."%"));

2 Comments

Hello @Marcelo what can use instead of Database class in the prepare statement? There was a compile error for the Database class.
@ThePredator ^^
3
SELECT * FROM Accounts WHERE Username LIKE '%$query%'

but it's not suggested. use PDO

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.