PHP - 使用空值查询

时间:2018-05-20 15:11:10

标签: php mysql sql

我想在this post中创建一个表单,但是我想这样做,如果其中一个输入是空的,那么php仍然会处理查询。我使用type Props = { push: $PropertyType<RouterState, 'mutationPush'> } 还是INNERJOIN

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

LEFTJOIN

它的PHP代码:

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

如果&#34;主题&#34;输入是空的,例如,我想让它$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(); 查询仍然会返回一行而不是什么

2 个答案:

答案 0 :(得分:0)

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

这是一个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";

然后将params绑定到查询语句。

call_user_func_array($stmt->bind_param, $where->params());

答案 1 :(得分:0)

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