3

我对 php 和一般编码相当陌生。

我有一系列条件需要测试它们是否已设置。它们是 $option1、$option2、$option3

if (isset($option1)){
    if (isset($option2)){
        if (isset($option3)){
            $query = "SELECT *
                               FROM Group
                               WHERE FirstOption = '$option1' AND SecondOption = '$option2' AND ThirdOption = '$option3'";
        }
        else {
            $query = "SELECT *
                               FROM Group
                               WHERE FirstOption = '$option1' AND SecondOption = '$option2";
        }
    }
    else {
        $query = "SELECT *
                           FROM Group
                           WHERE FirstOption = '$option1' AND ThirdOption = '$option3";
    }
}
else {
    // .. snip, much more duplicated code ..
}

所以这里的问题是我需要根据条件是否设置来对 mysql 进行唯一查询。但它们彼此之间有很多,我实际上有 7 种选择,所以分支绝对是巨大的!在几乎让自己感到困惑之后,我在三点停了下来。

必须有一种更优越的方式来写这个 - 任何人都可以帮助我更好地理解吗?

4

5 回答 5

4

无需动态 sql 构建。只需使用逻辑。

SELECT *
  FROM Group
 WHERE (FirstOption = '$option1' or '$option1' = '') 
   AND (SecondOption = '$option2' or '$option2' = '')
   AND (ThirdOption = '$option3' or '$option3' = '')

理想情况下,您会使用占位符和准备好的语句,但以上内容对于教学目的来说是最清楚的。

万一有人好奇 - 这没有显着的查询开销。任何现代数据库引擎都会将'$option1' = ''表达式优化为一个常量值,仅将其评估为布尔值一次。

于 2013-01-16T05:09:09.973 回答
3
$baseSQL = "SELECT * FROM Group";

if(isset($option1)){
    sprintf("%s world. Day number %u",$str,$number);
    $whereSQL[] = sprintf("FirstOption = '%s'",mysqli_real_escape_string($mysqli,$option1));
}
if(isset($option2)){
    $whereSQL[] = sprintf("SecondOption = '%s'",mysqli_real_escape_string($mysqli,$option2));
}
if(isset($option3)){
    $whereSQL[] = sprintf("ThirdOption = '%s'",mysqli_real_escape_string($mysqli,$option3));
}

if(isset($whereSQL)){
    $query = $baseSQL ." WHERE " . implode(" AND ",$whereSQL);
}
于 2013-01-16T05:01:50.707 回答
3

我的普通方式:

$cond=array();
$bind=array();
if(isset($option1))
{
    $cond[]="FirstOption=?";
    $bind[]=$option1;
}
if(isset($option2))
{
    $cond[]="SecondOption=?";
    $bind[]=$option2;
}
if(isset($option3))
{
    $cond[]="ThirdOption=?";
    $bind[]=$option3;
}
$sql="SELECT * FROM `group`";
if(count($cond)>0) $sql.=" WHERE ".implode(" AND ",$cond);
$stmt=$mysqli->prepare($sql);
if(count($bind)>0)
{
    foreach($bind as $idx=>$val)
    {
        $stmt->bind_param("s",$bind[$idx]);
    }
}
于 2013-01-16T05:03:34.447 回答
1

试试这个

$sql = "Select * from Group where 1=1 ";
$whr = "";

if(isset($option1)){
$whr .= " and FirstOption = $option1";
}
if(isset($option2)){
$whr .= " and SecondOption = $option2";
}
if(isset($option3)){
$whr .= " and ThirdOption = $option3";
}
// nth element ...

if(isset($optionN)){
$whr .= " and nTHOption = $optionN";
}

$sql .= $whr;

这样,您可以在 where 条件下向查询中添加任意数量的选项。

于 2013-01-16T05:15:44.817 回答
0
if(isset($option1)||isset($option2)||isset($option3)){
$sqlToAdd = (isset($option1)?" FirstOption=$option1":"");
$sqlToAdd .= ($sqlToAdd==""?"":" AND ").(isset($option2)?" SecondOption=$option2":"");
$sqlToAdd .= ($sqlToAdd==""?"":" AND ").(isset($option3)?" ThirdOption=$option3":"");
$sql = "SELECT *
                               FROM Group
                               WHERE $sqlToAdd";
}
于 2013-01-16T04:58:36.677 回答