我想搜索数据库,最多3个字匹配多个字段,我使用给定的代码:
$hotel_name=trim($_GET['hotel_src_key']);
$search_type=trim($_GET['hotel_src']);
$pieces = explode(" ", $hotel_name);
//echo $pieces[1]; // piece2
$search1=$pieces[0];
$search2=$pieces[1];
$search3=$pieces[2];
echo $search1.$search2.$search3;
if($search_type=="any hotel")
{
$rest ="WHERE hotel_name like '%$search1%' AND hotel_name like '%$search2%'AND hotel_name like '%$search3%' OR city like '%$search1%' AND city like '%$search2%' AND city like '%$search3%'";
}
if($search_type=="hotel city"){
$rest="WHERE city like '%$search1%' AND city like '%$search2%' AND city like '%$search3%'";
}
if($search_type=="hotel name"){
$rest="WHERE hotel_name like '%$search1%' AND hotel_name like '%$search2%' AND hotel_name like '%$search3%'";
}
$stmt=$connc->query("SELECT * FROM hotels $rest ORDER BY h_id ASC");
$rc = $stmt->num_rows;
但是,当我在"agra jaipur delhi"
搜索带有关键字(search_type=any hotel)
的酒店时,我的工作无效,因为我想要bcoz,结果会显示0
我该如何解决这个问题?
答案 0 :(得分:0)
WHERE hotel_name like :search1 OR hotel_name like :search2 or hotel_name like :search3
如果您使用的是PDO,请使用预备语句http://php.net/manual/en/pdo.prepare.php;如果您使用的是mysqli,请使用http://php.net/manual/en/mysqli.prepare.php