简体   繁体   English

PHP的搜索引擎的关键字不起作用

[英]php Search engine the keywords are not working

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();

    ?>
 <?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. 1)删除了正在定义的第二个$ query。 It selected all rows. 它选择了所有行。

2) Moved initial $i declaration. 2)移动了初始$ i声明。 It was being set back to 0 each loop. 每个循环将其设置回0。

3) Added WHERE 3)在哪里添加

4) Moved $i++ after the if statement and set initial $i to 1. 4)将$ i ++移动到if语句之后,并将初始$ i设置为1。

5) Added mysql_real_escape_string so that data is escaped properly. 5)添加了mysql_real_escape_string,以便正确地转义数据。

Recommendations: 建议:

I highly recommend taking a look at MySQLi ( http://us2.php.net/mysqli ) or PDO ( http://us3.php.net/pdo ) 我强烈建议您看一下MySQLi( http://us2.php.net/mysqli )或PDO( http://us3.php.net/pdo

Please let me know if this works or if you need further assistance. 请让我知道这是否可行,或者您需要进一步的帮助。

A first sight, i see a couple of errors. 乍一看,我看到几个错误。

 $i=0;
 $i++;
     if ($i == 1)

$i Will ALWAYS be one are. $ i将永远是一个。 you might want to move $i = 0; 您可能要移动$ i = 0; BEFORE the foreach 在foreach之前

$query = mysql_query("SELECT * FROM search");

You build a query, but in the end you're not using it. 您建立了一个查询,但是最后您没有使用它。 you probably want to do : $query = mysql_query($query); 您可能想做:$ query = mysql_query($ query); instead. 代替。 ( and also for code clarity using a different variable name for the output ? ) . (以及为了代码清晰起见,在输出中使用其他变量名?)。

mysql_query is deprecated. mysql_query已弃用。 Useless you're in a hurry, check PDO 如果您没用,请检查PDO

First, you're missing the WHERE keyword before the conditions. 首先,您在条件之前缺少WHERE关键字。 So it should be: 因此应该是:

    foreach ($terms as $i => $each){
        $each = mysql_real_escape_string($each); // Prevent SQL injection
        if ($i == 0)
            $query .= "WHERE keywords LIKE '%$each%' ";
        else
            $query .= "OR keywords LIKE '%$each%' ";

    }

You don't need to increment your own counter variable, you can use the array indexes from $terms . 您不需要增加自己的计数器变量,可以使用$terms的数组索引。

Second, after all that work to create $query , you're not using it. 其次,在完成$query所有工作之后,您就不再使用它了。 You wrote: 你写了:

    $query = mysql_query("SELECT * FROM search");

That should be: 应该是:

    $query = mysql_query($query);

BTW, it's generally a bad idea to reuse variables like that, it gets confusing when you use the same variable for different things. 顺便说一句,重用这样的变量通常是一个坏主意,当您将相同的变量用于不同的事物时,这会造成混乱。 I suggest you call the second $query something like $results . 我建议您将第二个$query称为$results

Change this line 更改此行

$query .= "keywords LIKE '%$each%' ";

By 通过

$query .= " Where keywords LIKE '%$each%' ";

And also cnhange this line 还有cnhange这条线

$query = mysql_query("SELECT * FROM search");

By 通过

$query = mysql_query($query);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM