php 选择其中行值包含字符串 MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6447899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:24:59  来源:igfitidea点击:

SELECT where row value contains string MySQL

phpmysql

提问by max_

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

例如,如何在我的 MySQL 数据库中选择一行,其中列的值包含“XcodeDev”?

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?

我能做些什么来实现我想要的?

回答by Matt Ball

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

使用%通配符,它可以匹配任意数量的字符。

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

回答by jncraton

This should work:

这应该有效:

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

回答by Marcelo

My suggestion would be

我的建议是

$value = $_POST["myfield"];

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

回答by dynamic

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

but it's not suggested. use PDO

但不建议这样做。使用 PDO