我有一张桌子,包括ID,标题和文本。从下拉菜单中,我允许用户选择标题和文本。根据标题和文本,我想显示搜索结果。但是,当我使用AND
运算符编写查询时,尽管匹配数据在数据库中可用,但它不会显示任何结果。有人可以帮我解决这个问题吗?
这是查询搜索代码
$search_value=$_POST["search"];
$services=$_POST["services"];
$sql="select * from articles where title like '%$search_value%' AND text like '%services%' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["title"]. $row["text"];
}
} else {
echo "0 results";
}
这是下拉列表
<form action="search.php" method="post">
<select class="form-control" name="search" id="select1">
<option value="Ampara">Ampara</option>
<option value="Anuradhapura">Anuradhapura</option>
<option value="Badulla">Badulla</option>
<option value="Batticaloa">Batticaloa</option>
<option value="Colombo">Colombo</option>
</select>
<select class="form-control" name="services" >
<option value="Architecture">Architecture</option>
<option value="Astrology">Astrology</option>
<option value="Electrical">Electrical</option>
<option value="Landscaping">Landscaping</option>
<option value="Hardware">Hardware</option>
<option value="Plumbing">Plumbing</option>
</select>
答案 0 :(得分:2)
请用以下内容替换您的查询, 您错过了查询中用于文本字段的$登录服务变量
$sql="select * from articles where title like '%" . $search_value . "%' AND text like '%" . $services . "%' ";