0

我正在尝试做一个简单的 if else 语句,但我无法获得预期的结果,并且显然使用了错误的方法。

这就是我所拥有的:

if (!$res7 and !$res8) {output one - this should appear if both $res7 and $res8 are empty}

else if (!$res8)  {output two - this should appear if $res8 is empty}

else if (!$res7) {output three - this should appear if $res7 is empty}

输出显示了我想要的,有人可以帮忙修复它吗?谢谢

编辑完整代码: $res7 和 $res8 是 MySQL 查询的结果

$res8 = $pdo->query($query8);

if (empty($res7) && empty($res8)) {

printf("We currently have no reviews for this park or its attractions. If you have been to %s please take a minute to give your feedback. You don't need to register to leave reviews or ratings. Thank you." . PHP_EOL, $row[name]);
}

elseif (empty($res8)) {

printf("We currently have no attraction reviews for this park. If you have been to %s please take a minute to give your feedback. You don't need to register to leave reviews or ratings. Thank you." . PHP_EOL, $row[name]);

}

elseif (empty($res7)) {

printf("<h3>Park Review</h3>We currently have no reviews for this park as a whole. If you have been to %s please take a minute to give your feedback. You don't need to register to leave reviews or ratings. Thank you." . PHP_EOL, $row[name]);
}

else { 
printf("all good");
}
4

2 回答 2

3

这就是你初始化的方式$res8

$res8 = $pdo->query($query8);

此时,$res8是一个结果集,包含您的查询结果。您要做的是检查该结果集是否为空。你不能调用empty()它,因为即使没有结果,它仍然是一个结果集,所以它在你的调用中返回 FALSE。

您需要做的是使用内置函数来查看它包含多少行:

if ($res8->rowCount() == 0) {
    ....
于 2013-08-20T17:44:12.577 回答
1

如果你的意思是空 asempty而不是 as false,那么这样做:

if (empty($res7) && empty($res8)) {
    // First case
} elseif (empty($res8)) {
    // Second case
} elseif (empty($res7)) {
    // Third case
} else {
    // Not $res7 nor $res8 are empty
}
于 2013-08-20T17:08:29.263 回答