php MYSQLi 从类似/包含的数据库结果中选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21053070/
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
MYSQLi select from database results that are like / contain
提问by user2001411
Since I am new to Mysqli in general I am not sure what to look for so I am posting here.
由于我通常是 Mysqli 的新手,因此我不确定要寻找什么,所以我在这里发帖。
TABLE = id | firstname | lastname | attribute1 | attribute 2
表 = id | 名字 | 姓氏 | 属性 1 | 属性 2
Example string to search for is "test" The script should search for any row where the firstname or lastname contain "test" or "tést" or "tèst" or a variation of this.
要搜索的示例字符串是“test” 该脚本应搜索名字或姓氏包含“test”或“tést”或“tèst”或其变体的任何行。
Then is should return all results in JSON format.
然后应该以 JSON 格式返回所有结果。
How would I go about doing this?
我该怎么做呢?
回答by Fabien Warniez
Well, MySQL to JSON is a big stretch.
好吧,从 MySQL 到 JSON 是一个很大的延伸。
Let's focus on the MySQL part here.
让我们在这里专注于 MySQL 部分。
- Query to filter on your keyword:
SELECT * FROM table WHERE firstname LIKE '%test%' OR lastname LIKE '%test%' - Then using mysqli you can retrieve the results.
- 查询以过滤您的关键字:
SELECT * FROM table WHERE firstname LIKE '%test%' OR lastname LIKE '%test%' - 然后使用 mysqli 你可以检索结果。
You have to do research on your own. Post specific questions or problems here. We can't just do it for you...
你必须自己做研究。在此处发布特定问题或问题。我们不能只为你做...
回答by frosty11x
What you need to do to search for a string is this
您需要做的是搜索字符串
SQL: SELECT * FROM TABLE WHERE firstname LIKE '%test%' OR lastname LIKE '%test%' OR attribute1 LIKE '%test%' OR attribute2 LIKE '%test%'
查询语句: SELECT * FROM TABLE WHERE firstname LIKE '%test%' OR lastname LIKE '%test%' OR attribute1 LIKE '%test%' OR attribute2 LIKE '%test%'
You can of course put a post variable in the like statement if you want so like '%".$_POST['value']."%'
如果您愿意,您当然可以在 like 语句中放置一个 post 变量 '%".$_POST['value']."%'
To put it in json use the php function json_encode.
要将其放入 json 中,请使用 php 函数json_encode。
回答by Muhammad Saud
SELECCT * FROM <TABLE NAME> WHERE firstname LIKE '%test%' OR lastname LIKE '%test%';
then use fetch array function and apply json_encode()function on that array to get the json string.
然后json_encode()在该数组上使用 fetch array 函数和 apply函数来获取 json 字符串。
回答by user2001411
Thanks for the help guys!
感谢您的帮助!
I was mainly looking for the SQL query
我主要是在寻找 SQL 查询
Using
使用
$results = mysqli_query($mysqli, "SELECT * FROM players WHERE firstname LIKE '%test%' OR lastname LIKE '%test%'");
$myarray = array();
while ($record = mysqli_fetch_assoc($results))
{
//print_r($record);
$temparray = $record;
array_push($myarray, $temparray);
}
echo json_encode($myarray);
回答by Kucing
I found it by myself. I hope this can help you as alternate coding.
我自己找到的。我希望这可以帮助您作为替代编码。
$temp_findcategory = $_POST['findcategory'];
$temp_findkeyword = "%".$_POST['findkeyword']."%";
$search = mysqli_query($conn,"SELECT * FROM table WHERE field1 like '$temp_findkeyword' order by '$temp_findcategory' asc");
$row_search = mysqli_fetch_assoc($search);

