gpt4 book ai didi

php - 具有 3 个字段的高级搜索,需要查询

转载 作者:太空宇宙 更新时间:2023-11-03 10:53:23 25 4
gpt4 key购买 nike

我有一个包含三个字段的高级搜索表单。用户可以使用任何字段或任何两个或所有字段来开始搜索。所以我想检查用户是否为特定字段输入了值,并根据通过一系列 if 循环呈现的值编写查询。但我很想知道 mysql 中是否有任何等效技术来避免 if 循环并在单个查询中实现搜索。?

以下是检查用户输入的示例。

if($school!="" & $district1!="" & $scl_type!="")
{
echo "3 fields available";
}
else if($school=="" & $district1!="" & $scl_type!="")
{
echo "school empty, district,schooltype available";
}
else if($school!="" & $district1=="" & $scl_type!="")
{
echo "school,school type available district empty";
}
else if($school!="" & $district1!="" & $scl_type=="")
{
echo "school,district only present,school type is empty";
}

.....

所以我在每个 if 语句中写了不同的查询。有捷径吗?

<----更新:--->我收到了这个查询,它工作正常。但是当同一地区有两所学校时,它只返回一所。即使有很多结果也只返回一个结果,怎么办?

SELECT * FROM `register` WHERE 
(schoolname IS NULL OR schoolname LIKE '%national%')
AND
(schooltype IS NULL OR schooltype LIKE '%state board%')
AND
(district IS NULL OR district LIKE '%thiruvarur%');

最佳答案

你试试这个

$where = array();
if($school)
{
$where[] = "school where condition";
}
if($district1)
{
$where[] = "district1 where condition";
}
if($scl_type)
{
$where[] = "scl_type where condition";
}
$where = implode(' and ',$where);

$query = "select * from tablename where ".$where;

关于php - 具有 3 个字段的高级搜索,需要查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22704550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com