/******
**一个简单的搜索
*****/
//将搜索语句加到最开始的查询语句里面;从而实现了代码的精简
$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";
//相当于重置了查询语句,从而达到搜索的目地
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。