简体   繁体   中英

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?

使用% wildcard ,它匹配任意数量的字符。

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

这应该工作:

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

My suggestion would be

$value = $_POST["myfield"];

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

but it's not suggested. use PDO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM