1

我在我的网站上有一个查询,如果没有结果,我想说点别的,而不是有一个空白页......

    $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++;
    }
4

3 回答 3

4
$sth->execute(array(':county' => $county));
if ($sth->rowCount() == 0) {
   echo 'no rows';
} else {
   while(yada yada yada) { ... }
}

相关文档: http: //php.net/manual/en/pdostatement.rowcount.php

于 2013-01-10T16:31:29.033 回答
2

您正在计算 中的结果$c,因此您可以通过在代码中附加以下内容来检查具有哪个值:

if($c == 1) { // Counter is stuck at 1
    echo "No results were found.";
}
于 2013-01-10T16:32:15.503 回答
-1

2 可能的解决方案,执行 SELECT COUNT 执行 fetchall 检查并稍后显示结果

于 2013-01-10T16:39:02.407 回答