dsa45664 2014-08-23 04:54
浏览 55
已采纳

php搜索引擎关键字不起作用

When i would search for the keywords that i have specified in my database it will return everything from my database not just the corresponding links that have the keywords attached to the link. here is my code

        <?php
        $q = $_GET['q'];
        $terms = explode(" ", $q);
        $query = "SELECT * FROM search ";

        foreach ($terms as $each){
            $i=0;
            $i++;
                if ($i == 1)
                    $query .= "keywords LIKE '%$each%' ";
                else
                    $query .= "OR keywords LIKE '%$each%' ";

        }

        //connect
        mysql_connect("localhost", "root", "");
        mysql_select_db("search");

        $query = mysql_query("SELECT * FROM search");
        $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 "<h3><a href='$link'>$title</a></h3><h4>$link</h4>$description<br /><br />";
            }
        }
        else
            echo "<b>No Results Found</b><br><br>Suggestions:<br>
                Make sure all words are spelled correctly.<br>
                Try different keywords.<br>
                Try more general keywords.";

        //disconnect
        mysql_close();

    ?>
  • 写回答

4条回答 默认 最新

  • dongre6404 2014-08-23 05:04
    关注
     <?php
        $q = $_GET['q'];
        $terms = explode(" ", $q);
    
        //connect
        mysql_connect("localhost", "root", "");
        mysql_select_db("search");
    
        $query = "SELECT * FROM search ";
    
        $i=1;
        foreach ($terms as $each){
            if ($i == 1) {
                $query .= "WHERE ";
                $query .= "keywords LIKE '" . mysql_real_escape_string("%" . $each . "%") . "' ";
            } else {
                $query .= "OR keywords LIKE '" . mysql_real_escape_string("%" . $each . "%") . "' ";
            }
            $i++;
        }
    
        $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 "<h3><a href='$link'>$title</a></h3><h4>$link</h4>$description<br /><br />";
            }
        } else {
            echo "<b>No Results Found</b><br><br>Suggestions:<br>
                Make sure all words are spelled correctly.<br>
                Try different keywords.<br>
                Try more general keywords.";
        }
        //disconnect
        mysql_close();
    
    ?>
    

    Fixes:

    1) Removed second $query that was being defined. It selected all rows.

    2) Moved initial $i declaration. It was being set back to 0 each loop.

    3) Added WHERE

    4) Moved $i++ after the if statement and set initial $i to 1.

    5) Added mysql_real_escape_string so that data is escaped properly.

    Recommendations:

    I highly recommend taking a look at MySQLi (http://us2.php.net/mysqli) or PDO (http://us3.php.net/pdo)

    Please let me know if this works or if you need further assistance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染