我有这张桌子
recipe_tbl
id | name | ingredients
--------------------
1 | Banku | corn dough, cassava dough, okra, tomato, onion, pepper, garden eggs
2 | Jollof | rice, tomato, pepper, onion, spice
3 | fufui | cassava, okra, plantain
我希望选择一种配方,其成分与使用提供的任何列表相匹配。
例如,如果用户搜索“coco,butter,okra”,他们应该获得ID为1和3的项目,因为它们都包含“OKRA”,如果他们搜索“胡椒,奶油,咖喱”,他们应该获得带有ID 1和2,因为它们包含“PEPPER”。
如果有人能帮助我,我会很高兴,谢谢
答案 0 :(得分:0)
取$ userInput =" coco,butter,okra" 使用Like setence可以让每个人在每行中都有这些名字 就像你想要的那样
SELECT * FROM recipe_tbl
WHERE ingredients like "%$userInput%"
答案 1 :(得分:0)
您可以使用like
运算符在php中构建select语句。在php中分离每个不同的搜索参数。我假设您正在寻找不同的id
而不是重复。
<?php
$arr = array("coco", "butter", "okra");
$length = count($arr)
$sql = "SELECT DISTINCT id FROM recipe_tbl WHERE ingredients LIKE "
for ($x = 0; $x < $length; $x++) {
if($x == 0) $sql .= "'%$arr[$x]%'";
$sql .= " OR ingredients LIKE '%$arr[$x]%'"
}
$conn = new mysqli($servername, $username, $password, $dbname);
$result = $conn->query($sql);
?>