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 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!