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.
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(''Connections/conn.php''); ?> <?php $colname_rs = $_GET[''key'']; //获得用户输入 $result = explode('','',$_GET[''key'']);//分解用户输入的多个关键词,存入$result数组 mysql_select_db($database_conn, $conn); //连接数据库
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 ''%$result[0]%'' OR content LIKE ''%$result[0]%''"; else //对其他关键词,使用UNION连接 $query_rs .= " UNION SELECT * FROM searchtable WHERE title LIKE ''%$result[$i]%'' OR content LIKE ''%$result[$i]%''"; }
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[''key''] ?>"> <input type="submit" value="Submit"> </div> </form> <p align="center"><B>当前关键词: <?php
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[''id'']; ?>"><?php echo $row_rs[''title'']; ?></a>(<?php echo $row_rs[''click'']; ?> | <?php echo $row_rs[''last_access'']; ?>)</p> <?php } while ($row_rs = mysql_fetch_assoc($rs)); ?> </body> </html> <?php mysql_free_result($rs); ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
