Foreach MySQL选择结果

时间:2019-02-15 13:18:55

标签: php html mysql

我想创建一个搜索字段,就像我搜索“菠菜,西瓜”一样,它将以“,”爆炸输入,并运行MySQL搜索数据库。这意味着SQL将以

运行
SELECT * FROM table
  WHERE vegetable LIKE '%Spinach%' OR fruits LIKE '%Spinach%'

SELECT * FROM table
  WHERE vegetable LIKE '%Watermelon%' OR fruits LIKE '%Watermelon%'

我的数据库表数据看起来像这样:

id  Vegetable  fruits
----------------------
1   Spinach    Apple
2   Cucumber   Orange
3   Spinach    Watermelon

每个id的结果只能显示一次。

<php>
$keywords = trim($_REQUEST['keyword']);
$keyword = explode(",", $keywords);
foreach($keyword as $keys=>$select)
{
   $sql = "SELECT * FROM table WHERE vegetable LIKE '%keyword %' OR fruits LIKE '%keyword %'";
   $result = mysqli_query($conn, $sql);
   while ($rs = mysqli_fetch_array($result))
   {    
       $vege = $rs["vegetable"];
       $fruits = $rs["fruits"];
   }
}

<html>
<form method=post>
    <input type="text" class="form-control" placeholder="SEARCH..." value="<?=$keywords?>">
</form>
</html>

4 个答案:

答案 0 :(得分:1)

在for循环中命中数据库不是一个好方法,您可以像这样构建查询:

<?php
$keywords = trim($_REQUEST['keyword']);
$keyword = explode(",", $keywords);
$sqlWhere = '';
foreach($keyword as $keys=>$select)
{
   $sqlWhere .= "LIKE '%$select%' OR";
}
$sqlWhere = rtrim($sqlWhere, "OR");

$sql = "SELECT * FROM table " . $sqlWhere;
$result = mysqli_query($conn, $sql);
while ($rs = mysqli_fetch_array($result))
{    
       $vege = $rs["vegetable"];
       $fruits = $rs["fruits"];
}    
?>

答案 1 :(得分:1)

如果您愿意交替使用REGEXP而不是LIKE,那么这里的方法很简单:

$keywords = trim($_REQUEST['keyword']);
$keywords = preg_replace("/,\s*/", "|", $keywords);
$where = "[[:<:]](" . $keywords . ")[[:>:]]";

$sql = "SELECT * FROM table ";
$sql .= "WHERE vegetable REGEXP '" . $where . "' OR fruits REGEXP '" . $where . "'";
$result = mysqli_query($conn, $sql);
while ($rs = mysqli_fetch_array($result)) {
    $vege = $rs["vegetable"];
    $fruits = $rs["fruits"];
}

假设您传递了关键字搜索字符串"Spinach, Watermelon",则上述脚本将生成以下查询:

SELECT *
FROM table
WHERE
    vegetable REGEXP '[[:<:]](Spinach|Watermelon)[[:>:]]' OR
    fruits REGEXP '[[:<:]](Spinach|Watermelon)[[:>:]]';

老实说,最好的方法是在准备好的语句中使用LIKEREGEXP。如果确实选择使用我的方法,那么绝对应该对传入的CSV字符串进行消毒,以确保其中仅包含字母数字字符,逗号和空格。

答案 2 :(得分:0)

尝试一下,它将打印您的记录。

<php>
$keywords = trim($_REQUEST['keyword']);
$keyword = explode(",", $keywords);
for($i=0; $i<count($keyword); $i++)
{
   $sql = "SELECT * FROM table WHERE vegetable LIKE '%" . $keyword[$i] . "%' OR fruits LIKE '%" . " . $keyword[$i] . " . "%'";
   $result = mysqli_query($conn, $sql);
   $rs = mysqli_fetch_array($result); 
   echo "<pre>"; print_r($rs); exit; 
}

?>
<html>
<form method=post>
    <input type="text" class="form-control" placeholder="SEARCH..." value="<?=$keywords?>">
</form>
</html>

答案 3 :(得分:0)

我认为您的想法是“ *每个ID的结果只能出现一次。”

因此最好进行一次查询:


$keywords = trim($_REQUEST['keyword']);
$keyword = explode(",", $keywords);
$where = [];
foreach($keyword as $keys)
{
   $where[] = "vegetable LIKE '%".$keys." %' OR fruits LIKE '%".$keys." %'";
}
$sql = "SELECT * FROM table ".(empty($where) ? '' : implode(' OR ', $where));

因此您只有一个查询和一个结果集。

别忘了转义您的关键字。