Home > Article > Backend Development > PHP implements search function code
This article mainly shares with you the PHP search function code. It mainly explains it with text and code. I hope it can help you.
/****** **一个简单的搜索 *****/ //将搜索语句加到最开始的查询语句里面;从而实现了代码的精简 $news_sql = "SELECT * from books where 1=1 ".$searchAddSql." order by book_number "; // $searchAddSql最开始是为空的,如果用户输入了搜索条件,$searchAddSql 就会变成一个包涵查询语句的字符串: //先判定用户是否输入了搜索条件 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."%')"; } //最开始的$news_sql = "SELECT * from books where 1=1 ".$searchAddSql." order by book_number ";就会变成: $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"; //相当于重置了查询语句,从而达到搜索的目地
Related recommendations:
Explanation on how to implement simple search function in PHP
Explanation on how to implement Baidu search function in JS
The above is the detailed content of PHP implements search function code. For more information, please follow other related articles on the PHP Chinese website!