【问题标题】:PHP - Query with empty valuesPHP - 使用空值查询
【发布时间】:2018-05-20 15:11:10
【问题描述】:

我想创建一个类似this post 的表单,但我想这样做,如果其中一个输入为空,那么 php 仍将处理查询。我使用INNERJOIN 还是LEFTJOIN

编辑: 这是来自that post的html表单:

<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();

例如,如果“主题”输入为空,我想让SELECT 查询仍然返回一行,而不是什么都没有

【问题讨论】:

  • PHP 处理与leftinner 连接无关。请添加代码,以便我们查看您实际在做什么。
  • @user3783243 代码添加
  • 所以您希望动态构建where?这不是joins 的用途。您应该在 PHP 中使用条件来动态构建 where 子句。
  • 使用pdo 而不是mysqli。它的占位符(:placeholder) 格式对于此任务更加灵活。

标签: php mysql sql


【解决方案1】:

您想为非空请求参数构建查询子句。

这里是一个 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,然后添加你的条件。

$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】:

    使用 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 );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 2013-07-06
      相关资源
      最近更新 更多