我目前正在构建一个网站,该网站依赖于通过搜索栏从本地主机数据库中提取信息。我目前让它正确地显示所有信息,但当搜索与数据库中的任何内容都不匹配时,它就什么都不显示,我该如何将其更改为"未找到结果"或类似的内容?我的代码是:
<?php
require_once 'connect.php';
if(isset($_GET['keywords'])){
$keywords = $db->escape_string($_GET['keywords']);
$query = $db->query("
SELECT movie, game
FROM movies
WHERE movie LIKE '%{$keywords}%'
");
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<h1><a href="index.php">Test</a></h1>
<div class="search">
<form action="search.php" method="get">
<input name="keywords" type="text" placeholder="Type something!" autocomplete="off" size="40">
<input type="submit" value="Search!">
</form>
</div>
<br />
<?php
if($query->num_rows){
while($r = $query->fetch_object()){
?>
<div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
<div class="result"> <a><?php echo $r->game; ?></a> </div><br />
</body>
</html>
<?php
}
}
}
很抱歉粘贴了整个页面,但我甚至不知道从哪里开始,因为我是一个PHP傻瓜。
if($query->num_rows){..}
条件将为真,如果查询结果有超过0个结果,所以只需使用else部分打印没有结果找到的消息
<?php
require_once 'connect.php';
if(isset($_GET['keywords'])){
$keywords = $db->escape_string($_GET['keywords']);
$query = $db->query("
SELECT movie, game
FROM movies
WHERE movie LIKE '%{$keywords}%'
");
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<h1><a href="index.php">Test</a></h1>
<div class="search">
<form action="search.php" method="get">
<input name="keywords" type="text" placeholder="Type something!" autocomplete="off" size="40">
<input type="submit" value="Search!">
</form>
</div>
<br />
<?php
if($query->num_rows){
while($r = $query->fetch_object()){
?>
<div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
<div class="result"> <a><?php echo $r->game; ?></a> </div><br />
</body>
</html>
<?php
}
}
else{
echo "No Result Found!";
}
}
<?php
require_once 'connect.php';
if(isset($_GET['keywords']))
{
$keywords = $db->escape_string($_GET['keywords']);
$query = $db->query("SELECT movie, game FROM movies WHERE movie LIKE '%{$keywords}%'");
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<h1><a href="index.php">Test</a></h1>
<div class="search">
<form action="search.php" method="get">
<input name="keywords" type="text" placeholder="Type something!" autocomplete="off" size="40">
<input type="submit" value="Search!">
</form>
</div>
<br />
<?php
if($query->num_rows)
{
while($r = $query->fetch_object())
{
?>
<div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
<div class="result"> <a><?php echo $r->game; ?></a> </div><br />
<?}
}
else {?>
<div class="noResult">No Results Found</div>
<?}?>
</body>
</html>
<?php }?>
styles.css(这里,使用这个类名,你可以设置任何你想要的属性。我给出了一些。示例。)
.noResult{
color: #800000;
font-size:14 px;
}
为了适应您可以使用的打开和关闭标签:
if($query->num_rows){
while($r = $query->fetch_object()){
?>
<div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
<div class="result"> <a><?php echo $r->game; ?></a> </div><br />
<?php
}
} else {
?>
<div>No results</div>
<?php
}
?>
</body>
</html>
在任何输出之后都要保留/body和/html标记。