Home  >  Article  >  Backend Development  >  PHP simple search function implementation explanation

PHP simple search function implementation explanation

韦小宝
韦小宝Original
2018-03-05 10:29:4716729browse

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 ";

$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."%')";
}

The first

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

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";

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!

Statement:
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