下面是一個非常基本的搜索功能腳本。使用if/else語句無法正常工作的PDO select查詢
它工作得很好,從結果確實獲取時應該得到,但的問題是,當查詢應返回零結果,<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
}
}
?>
我在這裏錯過了什麼?