PHP simple search function implementation explanation

韦小宝
Release: 2023-03-20 18:18:01
Original
16777 people have browsed it

We can see that many websites have the search function. So how does PHP implement search? In fact, PHP has a very simple way to implement the search function. Let’s do it below. Let’s see how PHP simple search function is implemented

Simple PHP search

Add the search statement to the beginning QueryInside the statement; thus achieving code simplification

$news_sql = "SELECT * from books where 1=1 ".$searchAddSql." order by book_number ";
Copy after login
Copy after login

$searchAddSqlIt is empty at first. If the user enters the search conditions, $searchAddSql
will become a string containing the query statement :

First determine whether the user has entered the search conditions

if(isset($_GET["searchText"]))
{
	$searchText = $_GET["searchText"];
	$searchAddSql = $searchAddSql." and (book_number like '%".$searchText."%' 
	or book_name like '%".$searchText."%'
	or book_sum like '%".$searchText."%'
	or book_author like '%".$searchText."%')";
}
Copy after login

The first

$news_sql = "SELECT * from books where 1=1 ".$searchAddSql." order by book_number ";
Copy after login
Copy after login

will become:

$news_sql="SELECT * from books where 1=1 and (book_number like '%中%' 
or book_name like '%中%' 
or book_sum like '%中%' 
or book_author like '%中%' ) order by book_number";
Copy after login

It is equivalent to resetting the query statement to achieve the purpose of search

There are other ways to implement PHP search, this This method is actually the simplest one.

Related articles:

php search function

php search plus pagination Code

The above is the detailed content of PHP simple search function implementation explanation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!