简体   繁体   English

使用变量运算符PHP PDO构建MySQL查询

[英]Building MySQL query with need for variable operators PHP PDO

Is there any standard or best way in PHP PDO database class to change the operator from "=" to "IS" when I don't know if the value I'm going to pass is NULL or not. 当我不知道要传递的值是否为NULL时,PHP PDO数据库类中是否有任何标准或最佳方法将运算符从“ =”更改为“ IS”。 Here is an example of what I mean. 这是我的意思的一个例子。

I have two variables. 我有两个变量。 I want a count of the results that match. 我想要计算匹配的结果。 Sometimes one or both of the variables can be NULL. 有时,一个或两个变量可以为NULL。 But MySQL requires a different operator to find NULL (IS NULL). 但是MySQL需要其他运算符才能找到NULL(IS NULL)。

I'm currently just creating another variable for the operator and when I build the query I inject it in as so: 我目前只是为运算符创建另一个变量,在构建查询时,我将其注入为:

  // First create a variable "=" or "is" depending on whether var1 and var2 are NULL or not
  if($var1 == NULL) $var1_operator = "IS";
  else $var1_operator = "=";
  if($var2 == NULL) $var2_operator = "IS";
  else $var2_operator = "=";

  // Build the query
  $query = 'SELECT count(*) as count FROM table 
  WHERE var1 '.$var1_operator.' :var1 AND var2 '.$var2_operator.' :var2';

  //  Conduct the query
  $prepare = $db_connection->prepare($query);
  $prepare->bindValue(':var1', $var1, PDO::PARAM_STR);
  $prepare->bindValue(':var2', $var2, PDO::PARAM_STR);
  $prepare->execute();
  $results = $prepare->fetch();

Is there another way to do this? 还有另一种方法吗? This works to bindValue using PARAM_STR even when the value is NULL and not really a string. 即使值是NULL而不是真正的字符串,也可以使用PARAM_STR绑定值。 The query returns the correct value count. 查询返回正确的值计数。 Just not sure if there is better practice for doing it. 只是不确定是否有更好的做法。

As you are using mysql, you can use its spaceship operator , <=> : 当您使用mysql时,可以使用它的飞船运算符 <=>

$query = 'SELECT count(*) FROM table WHERE var1 <=> ? AND var2 <=> ?';
$prepare = $db_connection->prepare($query);
$prepare->execute([$var1, $var2]);
$count = $prepare->fetchColumn();

But in general, if you need variable operators or other query parts, such a conditional query building is the only way. 但是通常,如果您需要变量运算符或其他查询部分,则这种条件查询构建是唯一的方法。

A more generalized solution can be found in my article How to create a WHERE clause for PDO dynamically : 在我的文章如何动态地为PDO创建WHERE子句中可以找到更通用的解决方案:

// always initialize a variable before use!
$conditions = [];
$parameters = [];

// conditional statements
if (!empty($_GET['name']))
{
    // here we are using LIKE with wildcard search
    // use it ONLY if really need it
    $conditions[] = 'name LIKE ?';
    $parameters[] = '%'.$_GET['name']."%";
}
if (!empty($_GET['sex']))
{
    // here we are using equality
    $conditions[] = 'sex = ?';
    $parameters[] = $_GET['sex'];
}
if (!empty($_GET['car']))
{
    // here we are using not equality
    $conditions[] = 'car != ?';
    $parameters[] = $_GET['car'];
}
// the main query
$sql = "SELECT * FROM users";
// a smart code to add all conditions, if any
if ($conditions)
{
    $sql .= " WHERE ".implode(" AND ", $conditions);
}
// the usual prepare/execute/fetch routine
$stmt = $pdo->prepare($sql);
$stmt->execute($parameters);
$data = $stmt->fetchAll();

so in these conditions above whatever logic could be implemented, including operator change or anything. 因此在以上条件下,可以执行任何逻辑,包括更改操作员或执行任何操作。

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

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