代码之家  ›  专栏  ›  技术社区  ›  Khang

php-空值查询

  •  0
  • Khang  · 技术社区  · 7 年前

    我想创建一个类似于 this post ,但我希望这样做,如果其中一个输入为空,那么php仍将处理查询。我用吗 INNERJOIN LEFTJOIN 是吗?

    编辑: 这是来自 that post 以下内容:

    <form action="results.php" method="GET">
      <input type="text" name="input">
      <input type="text" name="topic">
      <input type="text" name="location">
    </form>
    

    以及它的php代码:

    $db = new mysqli(*your database connection information here*);
    $input = $_GET['input']; //this is for the text input - ignore
    $topic = $_GET['topic']; // the first select box value which works well
    $location = $_GET['location']; //the second select box value which isn't being inserted into the query
    $combined = $input . $topic . $location;
    $terms = explode(" ", $combined);
    $stmt = $db->prepare("SELECT * FROM search WHERE input = ? AND topic = ? AND location = ?");
    $stmt->bind_param("sss", $input, $topic, $location);
    $stmt->execute();
    $stmt->close();
    

    例如,如果“topic”输入为空,我想使它成为 SELECT 查询仍将返回一行而不是一无所有

    2 回复  |  直到 7 年前
        1
  •  0
  •   Oluwafemi Sule    7 年前

    要为非空请求参数生成查询子句。

    这是一个 Where 类抽象where子句的构造。

    <?php
    
    class Where {
        private $values;
        private $types;
    
        static $VALUE_TYPES = [
            'string' => 's',
            'integer' => 'i',
            'double' => 'd',
            'blob' => 'b',
        ];
    
        function __construct()
        {
            $this->values = [];
            $this->types = '';
        }
    
        function addCondition($column, $operator, $value) 
        {
            if(!empty($value)) {
                $this->values["$column $operator ?"] = $value;
                $this->types .= static::$VALUE_TYPES[gettype($value)];
            }
            return $this;
        }
    
        function clause() 
        {
           $condition = join(' AND ', array_keys($this->values));
           if ($condition) {
               return "WHERE $condition";
           }
           return "";
        }
    
        function params()
        {
            return array_merge([$this->types], array_values($this->values));
        }
    }
    

    要使用这个类,需要初始化 在哪里? 然后加上你的条件。

    $where = new Where();
    $where->addCondition('input', '=', $input);
    $where->addCondition('topic', '=', $topic);
    $where->addCondition('location', '=', $location);
    

    以这种方式将子句追加到查询中。

    echo "SELECT * FROM search {$where->clause()}\n";
    

    然后将参数绑定到查询语句。

    call_user_func_array($stmt->bind_param, $where->params());
    
        2
  •  0
  •   Karlo Kokkak    7 年前

    使用PDO为这个任务提供更好的灵活性。下面的代码还使用if-else语句来构建所需的查询。

    更新代码:

    $db = new PDO('mysql:host=localhost;dbname=dbnme', 'root','password');
    
    $input = $_GET['input'];
    $topic = $_GET['topic'];
    $location = $_GET['location'];
    
    $sql_string = "SELECT * FROM search";
    $where_clause = "";
    
    if($input != ""){
        $where_clause .= "input = :input";
    }
    
    if($topic != ""){
        if($where_clause !="") $where_clause .= " AND ";
        $where_clause .= "topic = :topic";
    }
    
    if($location != ""){
        if($where_clause !="") $where_clause .= " AND ";
        $where_clause .= "location = :location";
    }
    
    $sql_string = $sql_string.($where_clause!=""?" WHERE ":"").$where_clause;
    
    $stmt = $db->prepare($sql_string);
    $stmt->bindParam(':input', $input);
    $stmt->bindParam(':topic', $topic);
    $stmt->bindParam(':location', $location);
    $stmt->execute();
    
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        print_r( $row );
    }