高级搜索3个字段,需要查询

时间:2014-03-28 04:58:44

标签: php mysql sql

我有一个包含三个字段的高级搜索表单。用户可以使用任何字段或任何两个或所有字段来开始搜索。所以我想检查用户是否为特定字段输入了值,并根据通过一系列if循环显示的值来编写查询。但是我很想知道在mysql中是否有任何equaudilent技术可以避免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语句中写了不同的查询。有没有捷径?

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

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%');

6 个答案:

答案 0 :(得分:2)

你试试这个

$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;

答案 1 :(得分:1)

您的参数 - @ school,@ schooltype,@ district

select items from yourtable where 
    (school like @school) 
        and 
    (schooltype like @schooltype) 
        and 
    (district like @district)

在传递参数时,如果任何字段为空,请创建相应的参数=' %%' ,对于任何输入的字段,请制作相应的参数="'" + enterdfieldtext +"'"

示例:在您的第二个条件中,查询将类似于

select * from table where  
    (school like '%%') 
        and 
    (schooltype like 'givenschooltype') 
        and 
    (district like 'givendistrict')

答案 2 :(得分:1)

查询取决于您的数据库结构,但脚本可能如下所示:

$data = filter_input_array(
    INPUT_GET,
    array(
        'street' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
        'district' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
        'scl_type' => FILTER_SANITIZE_FULL_SPECIAL_CHARS
    )
);

if($data){
    $condition = array();
    foreach($data as $key => $value){
        $condition[] = sprintf("`%s` = '%s'", $key, $value);
    }

    $SQL_QUERY = sprintf('SELECT * FROM database_table WHERE %s', implode(' AND ', $condition));
}

答案 3 :(得分:0)

将条件变量设为

if($school!="" & $district1!="" & $scl_type!="")
{
    $condition = "your condition";
}
else if($school=="" & $district1!="" & $scl_type!="")
{
    $condition = "your condition";
}
else if($school!="" & $district1=="" & $scl_type!="")
{
    $condition = "your condition";
}
else if($school!="" & $district1!="" & $scl_type=="")
{
    $condition = "your condition";
}

Write your single Query here

答案 4 :(得分:0)

您可能对三元陈述感兴趣:

    $condition1=$school===""?"1=1":"school=$school";
    $condition2=$district===""?"1=1":"district=$district";
    ...Etc

    SELECT * FROM database WHERE $condition1 AND $condition2 AND $etc...

基本上,三元陈述与此相同:

   if($school===""){
       $condition1="1=1";
   } else {
       $condition1="school=$school";
   }

但是在一条线上。您可以遍历每个条件并相应地设置$condition_X_个变量,然后在查询中将它们全部放在一起,并进行单个查询。

您将在查询之前想要实现某种注入保护 - 例如在分配变量$school时转义它们等等。

答案 5 :(得分:0)

谢谢你们,我得到了答案..

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%');