简体   繁体   English

MySQL 检查 null 如果值为空,则使用 php

[英]MySQL check if null if value is empty using php

I have multiple arrays similar to below.我有多个类似于下面的 arrays。

{
  ["sku_original"]=>
  string(33) "98iuoo"
  ["po"]=>
  string(5) "9897"
  ["ca"]=>
  string(3) "557"
  ["cl"]=>
  string(5) "33"
  ["aa"]=>
  string(3) "80"
  ["ad"]=>
  NULL
  ["da"]=>
  string(4) "143.9"
  ["dr"]=>
  NULL
  ["cors"]=>
  NULL
}

I want to use IS NULL condition when a value is null in the array else I have to check equal to condition if not empty.当数组中的值为 null 时,我想使用IS NULL条件,否则如果不为空,我必须检查equal to条件。 I have tried something like below.我尝试过类似下面的方法。

$po =  $option['po'] != NULL ? $option['po'] : NULL ;
$ca =  $option['ca'] != NULL ? $option['ca'] : NULL ;
$cl =  $option['cl'] != NULL ? $option['cl'] : NULL ;
$aa = $option['aa'] != NULL ? $option['aa'] : NULL ;
$ad = $option['ad'] != NULL ? $option['ad'] : NULL ;
$da = $option['da'] != NULL ? $option['da'] : NULL ;
$dr = $option['dr'] != NULL ? $option['dr'] : NULL ;
$cors = $option['cors'] != NULL ? $option['cors'] : NULL ;

The above code is not assigning value to a variable if option index is null I also tried is_null.如果选项索引是 null,上面的代码没有为变量赋值我也试过 is_null。

and SQL like below.和 SQL 如下所示。

$sql = "SELECT abc FROM `abc` WHERE `po` = $po AND `ca` = $ca AND `cl` = $cl AND `aa` = $aa AND `ad` = $ad AND `da` = $da AND `dr` = $dr AND `dr` = '$dr' AND `cors` = '$cors' `sku` = '$sku' ";

I have to check IS NULL in SQL when value is null else I have to use equal to当值为 null 时,我必须检查 SQL 中的IS NULL NULL 否则我必须使用equal to

As you iterate your options variable (assuming you have already validated all of the keys using a whitelist of table column names), conditionally write a static IS NULL expression into the WHERE clause or a bound parameter with a non-null value.当您迭代您的选项变量时(假设您已经使用表列名称的白名单验证了所有键),有条件地将 static IS NULL表达式写入 WHERE 子句或具有非空值的绑定参数。

// unconditionally include sku
$where[] = "sku = ?";
$params = ['s', $sku];
foreach ($option as $key => $value) {
    if ($value === null) {
        $where[] = "$key IS NULL";
    } else {
        $where[] = "$key = ?";
        $params[0] .= 's';
        $params[] = $value;
    }
}

$sql = "SELECT column_with_different_name_from_table
        FROM abc
        WHERE " . implode(" AND " , $where);
$stmt = $conn->prepare($sql);
$stmt->bind_param(...$params);
$stmt->execute();
$result = $stmt->get_result();

foreach ($result as $row) {
    echo $row['column_with_different_name_from_table'] . "<br>";
}

You can use NULL in prepared statements directly.您可以直接在准备好的语句中使用 NULL。 Heres a dynamic way to build your query这是构建查询的动态方法

// put all your variables in an array
$vars = array('po', 'ca', 'ab') ;

// iterate through and set up your key/value pairs
foreach ($vars as $var) {
  $val = (isset($option[$var]) && !is_null($option[$var])) ? $option[$var] : NULL;
  $q[] = " $var = ? ";
  $v[] = $val ;     
}

// set up the prepared statement
$query = "SELECT abc FROM `abc` WHERE " . implode(" AND " , $q) ;
$stmt = $mysqli->prepare($query);
$types = str_repeat("s", count($q)) ;
$stmt->bind_param($types, ...$v);
$stmt->execute();

If you merely want optinal criteria, you can handle this in SQL by asking "is the parameter set, then use it, else don't".如果您只想要可选标准,您可以在 SQL 中通过询问“是参数集,然后使用它,否则不要”来处理这个问题。

$sql = "SELECT abc FROM abc WHERE (po = $po OR $po IS NULL) AND (ca = $ca OR $ca IS NULL) ...";

which can also be written as也可以写成

$sql = "SELECT abc FROM abc WHERE po <=> COALESCE($po, po) AND ca <=> COALESCE($ca, ca) ...";

in MySQL (thus having each parameter only once in the query instead of having to use them twice).在 MySQL 中(因此每个参数在查询中只有一次,而不必使用它们两次)。

UPDATE: I have focused on the SQL in above answer.更新:我在上面的答案中专注于 SQL。 It has been mentioned that you should not build the query string by concatenation, but use prepared statements and bind the variables.已经提到您不应该通过连接来构建查询字符串,而是使用准备好的语句并绑定变量。 This is correct.这是对的。 I am not a PHP programmer, but it seems the difference in above assignments is mainly to replace the $ with a : .我不是 PHP 程序员,但似乎上述分配的区别主要是用:替换$ Something like this:像这样的东西:

$sql = "SELECT abc FROM abc WHERE po <=> COALESCE(:po, po) AND ca <=> COALESCE(:ca, ca) ...";
$query = $pdo->prepare($sql);
$query->bindParam(":po", $option["po"]);
$query->bindParam(":ca", $option["ca"]);
...
$query->execute();

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

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