PHP SQL用关键字搜索多个表并显示搜索结果

时间:2019-11-21 22:29:58

标签: php sql

我几乎希望能够使用多个表中的关键字进行搜索,然后显示每个表(产品,服务,博客文章)的匹配结果。我有可以在一个表中搜索的代码,并且在查询中摆弄了UNION的一些内容,但不确定从何处去。我对php不太有经验。如果有人可以向我展示如何将其与我的代码配合使用的示例

<?php
include "config.php";
include 'Header.php';

// declares two arrays
$results = array();
$errors = array();

// get search textbox
$searchTerms = mysqli_real_escape_string($link, $_GET['search']);

// check if the length is not less than 2 chars
if (strlen($searchTerms) < 3) {
   $errors[] = "Your search term must be longer than 2 characters";
}  

// if there is no error, let perform the search
if (count($errors) < 1) {

  $query = "(SELECT * FROM tbl_products WHERE product_name LIKE '%{$searchTerms}%' OR product_description LIKE '%{$searchTerms}%')
   UNION
   (SELECT * FROM tbl_blog WHERE blog_title LIKE '%{$searchTerms}%' OR blog_content LIKE '%{$searchTerms}%') 
   UNION
   (SELECT * FROM service_categories WHERE serviceCat_name LIKE '%{$searchTerms}%')";          
   $result = mysqli_query($link, $query);

   //checks if the search yields any result
   if (mysqli_num_rows($result) < 1) {
     $errors[] = "Your search term yielded no results!<hr>";
   }
   else {
     //loop and store through all the results 
     while ($row=mysqli_fetch_array($result)) {
       extract($row);
       $results[] = '<a class="searchPageBox" href="viewProperty.php?propertyID='.$product_ID.'"><div style="width: 100%;"><div class="row"><div class="col-md-4">'.'<img style="width:100%; height=100%;" src="product-images/'.$product_image.'"  /></div><div class="col-md-8"><h3 class="searchPageTitle">'.$product_name.'</h3><div class="searchPageDesc">'.$product_description.'</div></div></div></div></a><hr>';
     } // end of while

   } // end of else

} // end of if

?>
<!DOCTYPE html>
<html>
<head>
<title>Search Site</title>
</head>

<body class="homepage">
<div id="page">
    <div id="two-column1" class="container">
        <div id="colB">
            <h3 class="searchPropertiesHeading">SEARCH PROPERTIES</h3>
        </div>
        <div id="colA">
            <!-- PHP here -->
            <h2 class="searchResults">Search Results for
            <span style="font-weight: bold"><?php echo $searchTerms ?></span>:</h2>
            <div>
                <?php echo count($results) ?>results</div>
            <hr><?php
          // display the search results here
          if (count($results) >0) {
              echo "".implode("", $results);
          }

          // display the error here
          if (count($errors) >0) {
              echo "".implode("<br>", $errors);
          }

         ?></div>
    </div>
</div>
<?php 
include 'Footer.php'; 
?>
<script src="js/jquery-1.10.1.min.js" type="text/javascript"></script>
<script src="js/bootstrap.js"></script>
<script src="js/script.js" type="text/javascript"></script>

</body>

</html>

2 个答案:

答案 0 :(得分:0)

对于任何想知道查询结果如何的人:

$query = "(SELECT 'product' AS type, 'product' AS link, product_ID AS ID, product_name AS name, product_description AS description, product_image AS image FROM tbl_products WHERE product_name LIKE '%{$searchTerms}%' OR product_description LIKE '%{$searchTerms}%')
   UNION
   (SELECT 'blog' AS type, 'blog' AS link, blog_ID AS ID, blog_title AS name, blog_content AS description, blog_image AS image FROM tbl_blog WHERE blog_title LIKE '%{$searchTerms}%' OR blog_content LIKE '%{$searchTerms}%') 
   UNION
   (SELECT 'service' AS type, 'serviceCat' AS link, serviceCat_ID AS ID, serviceCat_name AS name, serviceCat_description AS description, serviceCat_image AS image FROM service_categories WHERE serviceCat_name LIKE '%{$searchTerms}%' OR serviceCat_description LIKE '%{$searchTerms}%') ORDER BY name ASC";     
   $result = mysqli_query($link, $query);


   if (mysqli_num_rows($result) < 1) {
     $errors[] = "Your search term yielded no results!<hr>";
   }
   else {
     //loop and store through all the results 
     while ($row=mysqli_fetch_array($result)) {
       extract($row);
       $results[] = '<a class="searchPageBox" href="view'.$type.'.php?'.$link.'ID='.$ID.'"><div style="width: 100%;"><div class="row"><div class="col-md-4">'.'<img style="width:100%; height=100%;" src="product-images/'.$image.'"  /></div><div class="col-md-8"><h3 class="searchPageTitle">'.$name.'</h3><div class="searchPageDesc">'.$description.'</div></div></div></div></a><hr>';
} 


     // end of while

   } // end of else

} // end of if

答案 1 :(得分:-1)

如果要在同一“表格”中显示所有结果(例如HTML表格,但是使用哪种可视化类型都没有关系),则需要为每个表格定义固定数量的列。 / p>

例如,如果您拥有产品(标识,名称,价格,描述,类别)和博客(标识,标题,正文),则可以使用3列(标识,类型和结果)进行合并。 / p>

  • ID:每种类型的唯一标识符。
  • TYPE:“博客”,“产品”等,您可能可以生成指向该商品的链接。
  • 结果:文本显示在何处,为什么选择此行。您可以使用||连接多个字段。

示例:

$query = "(SELECT id, 'product', product_name || ' ' || product_description FROM tbl_products WHERE product_name LIKE '%{$searchTerms}%' OR product_description LIKE '%{$searchTerms}%')
   UNION
   (SELECT id, 'blog', blog_title || ' ' || blog_content  FROM tbl_blog WHERE blog_title LIKE '%{$searchTerms}%' OR blog_content LIKE '%{$searchTerms}%') 
   UNION
   (SELECT id, 'services', serviceCat_name  FROM service_categories WHERE serviceCat_name LIKE '%{$searchTerms}%')";