Home > Backend Development > PHP Problem > How to implement multiple keyword queries in PHP?

How to implement multiple keyword queries in PHP?

coldplay.xixi
Release: 2023-03-02 11:46:01
Original
3359 people have browsed it

How to implement multiple keyword queries in PHP: 1. Use the like clause in the SQL statement to search the database; 2. Search for multiple keywords at the same time and use the union clause to merge the search results. That’s it.

How to implement multiple keyword queries in PHP?

PHP’s method of implementing multiple keyword queries:

PHP mainly uses SQL statements to search the database To achieve this, use the like clause. If you search for multiple keywords at the same time, you can use the union clause to combine the search results. The following code implements a search page.

Quote

<?php require_once(&#39;&#39;Connections/conn.php&#39;&#39;); ?>
<?php
$colname_rs = $_GET[&#39;&#39;key&#39;&#39;]; //获得用户输入
$result = explode(&#39;&#39;,&#39;&#39;,$_GET[&#39;&#39;key&#39;&#39;]);//分解用户输入的多个关键词,存入$result数组
mysql_select_db($database_conn, $conn); //连接数据库
Copy after login

Construct a SQL statement based on multiple keywords

$query_rs = "SELECT * FROM (";
for($i=0;$i<count($result);$i++) //根据每个搜索关键词构建SQL语句
{
if($i==0) //对第一个关键词,不使用UNION
$query_rs .= "SELECT * FROM searchtable WHERE title LIKE &#39;&#39;%$result[0]%&#39;&#39;
OR content LIKE &#39;&#39;%$result[0]%&#39;&#39;";
else //对其他关键词,使用UNION连接
$query_rs .= " UNION SELECT * FROM searchtable WHERE title LIKE
&#39;&#39;%$result[$i]%&#39;&#39; OR content LIKE &#39;&#39;%$result[$i]%&#39;&#39;";
}
Copy after login

Execute the SQL statement to sort the search results

$query_rs .= ") T ORDER BY last_access DESC";
$rs = mysql_query($query_rs, $conn) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);
?>
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form name="form1" method="get" action="?">
<div align="center">请输入要搜索关键词:
<input name="key" type="text" size="64" value="<?php echo $_GET[&#39;&#39;key&#39;&#39;] ?>">
<input type="submit" value="Submit">
</div>
</form>
<p align="center"><B>当前关键词:
<?php
Copy after login

Loop display of keywords

for($i=0;$i<count($result);$i++) { 
echo $result[$i]." ";
}
?></B></p>
<p><hr></p>
<?php if($totalRows_rs>0) do { //显示当前搜索结果 ?>
<p>* <a href="show.php?key=<?php echo $colname_rs ?>&id=<?php echo
$row_rs[&#39;&#39;id&#39;&#39;]; ?>"><?php echo $row_rs[&#39;&#39;title&#39;&#39;]; ?></a>(<?php echo
$row_rs[&#39;&#39;click&#39;&#39;]; ?> | <?php echo $row_rs[&#39;&#39;last_access&#39;&#39;]; ?>)</p>
<?php } while ($row_rs = mysql_fetch_assoc($rs)); ?>
</body>
</html>
<?php
mysql_free_result($rs);
?>
Copy after login

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of How to implement multiple keyword queries in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template