动态构建SQL搜索查询?

时间:2015-09-22 07:09:40

标签: php mysql sql arrays

我正在尝试根据用户的输入构建搜索查询,但我遇到了ANDWHERE关键字的问题。这是代码:

if (isset($_POST['submitBtn'])) {

    $gender = $_POST['gender'];
    $level = $_POST['level'];
    $status = $_POST['status'];

    $query = 'SELECT * FROM candidate ';
    $where = array();

    $criteria = array('gender' => $gender, 'level' => $level, 'status' => $status);
    foreach ($criteria as $key => $value) {
        if ($value !== 'all') {
            $where[] = $key . ' = ' . $value;
        }
    }

}

输出如下:

Array
(
    [0] => gender = masculine
    [1] => level = low
    [2] => status = future
)

如果未选择任何选项,则默认为“全部”,并将其排除在$where[]之外。

我需要实现这一点,或类似的东西:

Array
(
    [0] => WHERE gender = masculine
    [1] => AND level = low
    [2] => AND status = future
)

仅当选择了一个或多个选项时才必须附加WHERE,并且只有在选择了两个或更多选项时才必须附加AND

在我使用的代码中,我有9个搜索输入。为了清楚起见,我只在代码段中显示了三个。你能帮我解决这个问题吗?

4 个答案:

答案 0 :(得分:0)

试试这个:我认为你需要字符串中的whereClause不在数组中,在这里你可以选择两个中的任何一个并删除另一个。

<?php 
$where=array();$flag=0;// use flag to identify the where/and
 $whereClause="";
 $criteria = array('gender' => "masculine", 'level' => "low", 'status' => "future");
    foreach ($criteria as $key => $value) {
        if ($value !== 'all') {
            if($flag == 0){
             $where[] = " WHERE " .$key . ' = ' . $value;//if you need array
             $whereClause='WHERE '.$key . ' = "' . $value.'"';//if you need string
            }else{
             $where[] = " AND " .$key . ' = ' . $value;
             $whereClause .=' AND '.$key . ' = "' . $value.'"';
            }
             $flag++;
        }
    }
    echo "<pre>";
    print_r($where);
    echo "</pre>";

   echo $whereClause;
?>

答案 1 :(得分:0)

您需要输入一个增量符($inc),然后将条件设置为:

$inc=1;
foreach ($criteria as $key => $value) {
        if ($value !== 'all') {
            if($inc==1){
            $where[] = 'Where '.$key . ' = ' . $value.'';
            }else{
            $where[] = 'AND '.$key . ' = ' . $value.'';
            }

            $inc++;
    }
}

在我看来,还有一种更清晰的方法:

if (isset($_POST['submitBtn'])) {

    $gender = $_POST['gender'];
    $level = $_POST['level'];
    $status = $_POST['status'];

    $query = 'SELECT * FROM candidate ';
    $where = array("Where 1=1");

    $criteria = array('gender' => $gender, 'level' => $level, 'status' => $status);
    foreach ($criteria as $key => $value) {
        if ($value !== 'all') {
            $where[] = 'AND '.$key . ' = ' . $value.' ';
        }
    }

}

答案 2 :(得分:0)

你可以这样做:

 $query = 'SELECT * FROM candidate ';
 $where='';
 $criteria = array('gender' => $gender, 'level' => $level, 'status' => $status);
 foreach ($criteria as $key => $value) {
    if ($value !== 'all') {
        if($where=='')
            $where='WHERE '.$key . ' = ' . $value;
        else
            $where.=' AND '.$key . ' = ' . $value;

    }
}
$query.=$where; //final query

答案 3 :(得分:0)

您也可以使用简单的开关语句

<?
    if (isset($_POST['submitBtn'])) {

        $gender = $_POST['gender'];
        $level = $_POST['level'];
        $status = $_POST['status'];

        $query = 'SELECT * FROM candidate ';
        $where = array();

        $criteria = array('gender' => $gender, 'level' => $level, 'status' => $status);
        foreach ($criteria as $key => $value)
        {
            if ($value !== 'all')
            {
                switch ($key)
                {
                    case 1:
                    {
                        $query = " WHERE " .$key . ' = ' . $value;
                        break;
                    }

                    case 2:
                    {
                        $query = " AND " .$key . ' = ' . $value;
                        break;
                    }

                    case 3:
                    {
                        $query = " AND " .$key . ' = ' . $value;
                        break;
                    }

                }
                $where[] = $query;
            }
        }

    }