开发者

SELECT where row value contains string MySQL

开发者 https://www.devze.com 2023-03-14 15:53 出处:网络
How can I select a row in my MySQL DB where the value of a column contains \'XcodeDev\' for example? I tried:

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?


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

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


This should work:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消