我在php中制作搜索引擎,但我没有收到0或者结果。当我搜索我的数据库中的关键字时,它显示我没有结果。任何人都可以帮助我,我做错了。下面是我的代码。
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = ("SELECT * FROM search WHERE ");
foreach($terms as $each){
$i++;
if($i == 1)
$query.= (" 'keywords' LIKE '%$each%' ");
else
$query.= (" OR 'keywords' LIKE '%$each%' ");
}
//Connect to db
require("./db.php");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if( $numrows>0 ){
while( $row = mysql_fetch_assoc($query) )
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h2><a href = '$link'>$title</a></h2> $description </br> </br>";
}
else
echo "No results were found for \"<b>$k</b>\"";
//Disconnect to db
mysql_close();
?>
答案 0 :(得分:0)
如tadman的评论中所述,您需要转义输入(提示,使用mysqli函数并使用mysqli_real_escape_string或pdo)。
话虽如此,我认为您需要删除关键字列名称中的引号,如下所示:
if($i == 1)
$query.= (" keywords LIKE '%$each%' ");
else
$query.= (" OR keywords LIKE '%$each%' ");