Php Sql使用关键字搜索多个表并显示搜索结果
我非常希望能够在多个表中使用关键字进行搜索,然后显示来自每个表产品、服务和博客文章的匹配结果。我有一些代码可以在一个表中搜索,并且在查询中对UNION做了一些修改,但不确定从这里可以走到哪里。我对php不是很有经验。如果有人能给我看一个例子,说明它是如何与我的代码一起使用的,这真的很有帮助Php Sql使用关键字搜索多个表并显示搜索结果,php,sql,Php,Sql,我非常希望能够在多个表中使用关键字进行搜索,然后显示来自每个表产品、服务和博客文章的匹配结果。我有一些代码可以在一个表中搜索,并且在查询中对UNION做了一些修改,但不确定从这里可以走到哪里。我对php不是很有经验。如果有人能给我看一个例子,说明它是如何与我的代码一起使用的,这真的很有帮助 <?php include "config.php"; include 'Header.php'; // declares two arrays $results = array(); $errors
<?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>
如果希望在同一个表(例如HTML表)中显示所有结果,但不管使用哪种类型的可视化,则需要为每个表定义固定数量的列 例如,如果您有产品id、名称、价格、描述、类别和博客id、标题、正文,那么您可以使用3列id、类型和结果将它们合并 ID:每种类型的唯一标识符。 类型:博客、产品等,可能是您可以生成项目链接的内容。 结果:其中的文本显示选择此行的原因。可以用| |连接多个字段。 例如:
$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}%')";
对于任何想知道结果查询结果的人:
$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
团结就是解决办法。但是它要求所有子查询返回相同数量的列,具有相同的数据类型。好的,谢谢。我对如何做这件事一无所知。是否有人可以使用我的代码提供一个示例?我添加了一个示例。作为一个普通表,按列名。可以使用AS设置列名。例如column1 | | column2作为新的|列。我已经让它工作并显示结果,感谢您的帮助。但是,我需要了解如何更改显示的链接,以便单击结果将用户带到相应的页面。有什么想法吗?