[英]If no results in table show X, PHP MySQL
我在我的網站上有一個查詢,如果沒有結果,我想說些別的,而不是空白頁...
$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) { ... }
}
您要在$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.