我正在尝试制作一个简单的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");
}
答案 0 :(得分:3)
这是您初始化$res8
$res8 = $pdo->query($query8);
此时,$res8
是一个结果集,包含查询结果。您要做的是检查结果集是否为空。您无法在其上调用empty()
,因为即使没有结果,它仍然是结果集,因此它在您的通话中返回FALSE。
您需要做的是使用内置函数来查看它包含的行数:
if ($res8->rowCount() == 0) {
....
答案 1 :(得分:1)
如果你的意思是空empty
而不是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
}