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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

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

Method 2

This should work:

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

Method 3

My suggestion would be

$value = $_POST["myfield"];

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

Method 4

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

but it’s not suggested. use PDO


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x