0

我正在用 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();

    ?> 
4

1 回答 1

0

正如 tadman 在评论中提到的,您需要转义您的输入(提示,使用mysqli函数并使用mysqli_real_escape_string或 pdo)。

话虽如此,我认为您需要从关键字列名称中删除引号,如下所示:

if($i == 1)
    $query.= (" keywords LIKE '%$each%' ");
else
    $query.= (" OR keywords LIKE '%$each%' ");
于 2013-07-25T17:57:38.290 回答