【发布时间】:2015-02-28 23:10:42
【问题描述】:
我有一个 正在 工作的搜索引擎,但只有当我搜索 1 个单词时。每当我搜索多个关键字时,我只会得到 1 个结果。
示例:在我的数据库中,我有像 'test' 和 'hello' 这样的标签; 每当我输入“test hello”并单击“搜索”时,它都会显示:
1 个结果你好(这是带有标签 = hello 的帖子的标题)。
我的代码(search.php - 我得到搜索结果的页面):
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(!$button) {
echo "you didn't submit a keyword";
} else {
if(strlen($search)<=1) {
echo "Search term too short";
} else {
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","root");
mysql_select_db("myschool");
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each) {
$x = NULL;
$construct = NULL;
$x++;
if($x==1) {
$construct .="tag LIKE '%$search_each%'";
} else {
$construct .="OR tag LIKE '%$search_each%'";
}
$construct ="SELECT * FROM posts WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0) {
echo "Sorry, there are no matching result for <b>$search</b>.";
} else {
echo "$foundnum results found !<p>";
while($runrows = mysql_fetch_assoc($run)) {
$title = $runrows ['title'];
$tag = $runrows ['tag'];
echo "<a href='#'><b>$title</b></a><br>";
}
}
}
}
}
?>
问题可能出在$x=++ 部分,因为我认为引擎没有显示甚至没有搜索数据库中的所有行,并且当行数> 1 时也没有显示。
提前谢谢各位。
编辑:
我现在使用上面的代码得到多个结果,但我以这种形式得到它们:
您搜索的是 hello test postare
找到 1 个结果! 你好 找到 1 个结果!
测试 找到 1 个结果!
postare noua
我怎样才能让它在一个地方添加结果,而不是每次找到不同关键字的新结果时都说出来?
【问题讨论】:
-
使用
mysqli而不是mysql。您的数据库连接应该在单独的安全页面上。正确的 URL 中不会有空格。查看rawurldecode()。为什么查询会包含WHERE OR?。 -
谢谢,但它仍然没有回答我的问题,为什么我没有得到多个关键字的多个结果?
-
查看全文搜索。