简体   繁体   English

动态构造 SQL 搜索查询?

[英]Dynamically construct SQL search query?

I am trying to build a search query based on the input from users, but I am having problems with the AND and WHERE keywords.我正在尝试根据用户的输入构建搜索查询,但我在使用ANDWHERE关键字时遇到了问题。 Here is the code:这是代码:

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

}

The output looks like this:输出如下所示:

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

If no option is selected, it defaults to 'all' and it is excluded from the $where[] .如果没有选择任何选项,它默认为 'all' 并且它被排除在$where[]

I need to achieve this, or anything similar:我需要实现这一点,或任何类似的:

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

The WHERE must be appended only if one or more options have been selected and the AND must be appended only if two or more options have been selected.WHERE只有当一个或多个选项已经被选中必须附加和AND只有当两个或两个以上选择了上必须附加。

In the code I am using I have 9 search inputs.在我使用的代码中,我有 9 个搜索输入。 To keep it clear I only displayed three in the snippet.为了清楚起见,我只在代码段中显示了三个。 Can you please help me figure this out?你能帮我解决这个问题吗?

Try this:I think you need the whereClause in string not in array,here you can choose any one from two and remove the other one.试试这个:我认为你需要字符串中的 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;
?>

You need to put one incrementer ( $inc ) and then put the conditions as:您需要放置一个增量器( $inc ),然后将条件设置为:

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

            $inc++;
    }
}

In My view there is one more clean way of achiving this:在我看来,还有一种更干净的方法可以做到这一点:

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.' ';
        }
    }

}

You can do this :你可以这样做 :

 $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

You can use simple switch statement too您也可以使用简单的 switch 语句

<?
    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;
            }
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM