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
SELECT where row value contains string MySQL
提问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
回答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