我目前正在构建一个依赖于通过搜索栏从我的localhost数据库中提取信息的网站。我目前正确显示所有信息,但是当搜索与我的数据库中的任何内容都不匹配时,它只显示任何内容,如何将其更改为“未找到结果”或类似的内容?我的代码是:
<?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菜鸟。
答案 0 :(得分:4)
if($query->num_rows){..}
条件为真,所以只需使用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!";
}
}
答案 1 :(得分:0)
<?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;
}
答案 2 :(得分:0)
以适合您可以使用的开始和结束标记:
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标记。