我有一个简单的 php mysql 搜索来搜索数据库,但是,我似乎无法让它从搜索字段和类别中搜索文本,但我只希望它在选择时搜索类别,
这是我尝试过的代码:
if ($search !== null){
$sql = "SELECT * FROM people WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE) ORDER BY price ".$price." LIMIT ".$start.", ".$limit;
$q = $conn->prepare($sql) or die("failed!");
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->execute();
}
if ($search !== null && $category !== null){
$sql = "SELECT * FROM people WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE) and category=:category ORDER BY price ".$price." LIMIT ".$start.", ".$limit;
$q = $conn->prepare($sql) or die("failed!");
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->bindParam(':category', $category, PDO::PARAM_STR);
$q->execute();
}
但是,如果选中,这仍然会忽略类别字段选项!有任何想法吗?
它们的设置如下:
// Check and set category
$category = (!empty($_GET['category']) ? $_GET['category'] : null);
// Check and set search
if(!empty($_GET['search'])){
$search = $_GET['search'];
}else{
$search = null;
}