【发布时间】:2013-11-30 12:44:58
【问题描述】:
我已经创建了搜索引擎,因此当有人在搜索框中输入内容时,它会从我的数据库中获取数据并以图像的形式显示结果。
一切正常,但如果我在数据库中有名称为“Lamborghini”的数据,当有人在搜索框输入“lambo”或“Lamborghini”时,它工作正常并显示结果,但当他输入“Lamborghini car”时它没有显示任何结果。
这是我的 PHP 代码:
<html>
<link href="css/imgpages.css" rel="stylesheet" type="text/css">
<head>
<title>Search the Database</title>
</head>
<body>
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
include("connection.php");
$term = $_POST['term'];
$query = mysql_query("select * from save_data where Title like '%$term%'");
while($row = mysql_fetch_array($query)) {
$post_id = $row['ID'];
$title = $row['Title'];
$image = $row['Name'];
?>
<div id="body">
<a href="pictures.php?title=<?php echo $title; ?>">
<div id="title"><?php echo $title; ?></div></a>
<a href="pictures.php?title=<?php echo $title; ?>">
<img src='uploads/<?php echo $image; ?>' width='140' height='140'></a>
</div>
<?php } ?>
【问题讨论】:
-
你真的应该对你的 SQL 注入漏洞做点什么。与数据库交互时使用
mysqli或PDO扩展名。你可以在这里阅读更多:php.net/manual/en/intro.mysql.php. -
这可能是因为您的数据库中没有包含“兰博基尼汽车”的标题,因此找不到它。
-
是的,但 Google 不仅仅使用
like '%$term%' -
考虑使用全文搜索系统,如 Zend Lucene,或者可能在 MySQL 中使用全文匹配。
LIKE语句只会进行琐碎的匹配(例如,如果关键字反转,将找不到匹配项)。 -
搜索引擎在哪里?