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

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条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历