max_
max_

Reputation: 24481

SELECT where row value contains string MySQL

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?

Upvotes: 52

Views: 182023

Answers (4)

Marcelo
Marcelo

Reputation: 71

My suggestion would be

$value = $_POST["myfield"];

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

Upvotes: 7

dynamic
dynamic

Reputation: 48091

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

but it's not suggested. use PDO

Upvotes: 3

jncraton
jncraton

Reputation: 9132

This should work:

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

Upvotes: 18

Matt Ball
Matt Ball

Reputation: 359776

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

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

Upvotes: 100

Related Questions