合并if语句

时间:2018-06-26 06:31:10

标签: php

我基本上是在尝试将这两个if语句合并为一个。 如果两个或两个陈述均正确,我希望能够发送邮件,否则不要发送。

我的搜索>>

$search1 = mysqli_query($link, "SELECT * FROM NND where dname like 'EXP%' and Bl = 1 ");
$search2 = mysqli_query($link, "SELECT * FROM NND where dname like 'EXP%' and Bl = 0");

$lb = $search1->num_rows;
$db = $search2->num_rows;

如果是我的

    if (mysqli_num_rows($search1) == 0) {
    echo ("There is nothing to send");
} else {
    (mail ($to, $subject, $message, $headers));
}

    if (mysqli_num_rows($search2) == 0) {
    echo ("There is nothing to send");
} else {
    (mail ($to, $subject, $message, $headers));
}

谢谢大家的光临

3 个答案:

答案 0 :(得分:1)

这是另一个变体,它使用来自两个查询的行计数之和:

if (mysqli_num_rows($search1) + mysqli_num_rows($search2) == 0) {
    echo ("There is nothing to send");
}
else {
    (mail ($to, $subject, $message, $headers));
}

但是我认为更好的方法是改为运行单个MySQL查询:

SELECT *,
    CASE WHEN dname Bl = 1 THEN 1 ELSE 2 END AS search  -- keep track of result set
FROM NND
WHERE
    dname LIKE 'EXP%' AND Bl = IN (0, 1);

然后,您可以只检查单个查询的总行数来做出决定。请注意,从延迟的角度来看,这种方法也很有吸引力,因为它只需要单次往返数据库。

答案 1 :(得分:0)

使用此代码:-

if (mysqli_num_rows($search1) == 0 && mysqli_num_rows($search2) == 0) {
    echo ("There is nothing to send");
} else {
    (mail ($to, $subject, $message, $headers));
}

答案 2 :(得分:0)

使用:

  if (mysqli_num_rows($search1) != 0 || mysqli_num_rows($search2) != 0) {
    (mail ($to, $subject, $message, $headers));    
   } else {
        echo ("There is nothing to send");
   }

或者您可以使用@rawathemant所说的话:

if (mysqli_num_rows($search1) == 0 && mysqli_num_rows($search2) == 0) {
    echo ("There is nothing to send");
   } else {
        (mail ($to, $subject, $message, $headers));    
   }

根据德=摩根定律:-)