下面是一个非常基本的搜索功能的脚本。
它工作得很好,从某种意义上说,结果确实会在应该的时候被获取,但问题是当查询应该返回零结果时,<div id="bar">No results!</div>
却没有显示出来。相反,页面是空白的。
$search_query = $_GET['search'];
$search_query = htmlentities($search_query);
$word = "%$search_query%";
$search=$sth->con->prepare("SELECT id, firstname, lastname FROM users WHERE
firstname LIKE ? OR
lastname LIKE ?");
$search->bindValue(1, $word, PDO::PARAM_STR);
$search->bindValue(2, $word, PDO::PARAM_STR);
$search->execute();
$results = $search->fetchAll();
foreach ($results as $row) {
$firstname = $row["firstname"];
$lastname = $row["lastname"];
if (!(count($results) == 0)) {
?>
<div id="foo">Here are your results</div>
<?php
} else {
<div id="bar">No results!</div>
<?php
}
}
?>
我在这里想念什么?