[英]If no results in table show X, PHP MySQL
I have a query on my site and if there is no results, I'd like to say something else rather than have a blank page... 我在我的网站上有一个查询,如果没有结果,我想说些别的,而不是空白页...
$sth = $conn->prepare("SELECT * FROM directory WHERE user_active != '' AND County = :county");
$sth->execute(array(':county' => $county));
$c = 1;
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
RETURNED DATA GOES HERE
$c++;
}
$sth->execute(array(':county' => $county));
if ($sth->rowCount() == 0) {
echo 'no rows';
} else {
while(yada yada yada) { ... }
}
Relevant docs: http://php.net/manual/en/pdostatement.rowcount.php 相关文档: http : //php.net/manual/en/pdostatement.rowcount.php
You're counting the results in $c
, so you can check which value that has by appending your code with this: 您要在
$c
计算结果,因此可以通过在代码后附加以下内容来检查具有哪个值:
if($c == 1) { // Counter is stuck at 1
echo "No results were found.";
}
2种可能的解决方案,执行SELECT COUNT执行fetchall检查并稍后显示结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.