这是我制作的一些PHP代码(随数据库提供)。我的问题是,当我搜索时,它只显示表中的1个结果,即使它们更多。例如,如果我搜索david,那么该表具有名称(david star和david管道工),它只显示其中一个而不是两者。非常感谢任何可能的帮助。感谢。
<?php
//load database connection
$host = "localhost";
$user = "root";
$password = "";
$database_name = "autocomplete";
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user,
$password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
// OR author LIKE '%$search%' LIMIT 0 , 10
$search=$_POST['studentnum'];
$query = $pdo->prepare("select *
from search, search_and_highlight
where Name LIKE '%$search%'");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
if (empty($search)) {
echo "<p>You forgot to enter a search term!!!";
} else
if ( $results = $query->fetch()) {
echo ' <div class="tm-sidebar-pad-2">
<a href="#" class="media tm-media tm-recommended-item">
<img src="img/img-01.jpg" alt="Image">
<div class="media-body tm-media-body tm-bg-gray">
<h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">'.$results['Name'].'</h4>
<h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">
echo </h4>
<h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">Kategorie</h4>
<h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">. </h4>
<h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">. </h4>
<h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">Kategorie</h4>
<h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">. </h4>
<h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">Preis</h4>
</div>
</a>
</div>';
} else {
echo 'Nothing found';
}
?>
答案 0 :(得分:0)
要获取多条记录,您需要循环结果记录集:
while ($result = $stmt->fetch( PDO::FETCH_ASSOC )) {
print $result['Name'];
}