PHP搜索脚本不起作用

时间:2014-04-24 15:12:40

标签: php mysql

我正在尝试运行一个简单的PHP搜索脚本,它将显示我的mySQL数据库,并在我创建的单独页面上显示结果。当我运行代码时,我的结果不会显示出来。有人可以帮帮我吗??这是我目前正在使用的代码:

<?php

    error_reporting(E_A`enter code here`LL);
    ini_set('display_errors', 1);

        $k = $_GET['k'];
        $terms = explode(" ", $k);
        $query = "SELECT * FROM search WHERE ";

        $i = 0;
        foreach ($terms as $each){
            $i++; 

            if ($i == 1) 
            {
                $query .= "keywords LIKE '%$each%' ";
            }           
            else
            {
                $query .= "OR keywords LIKE '%$each%' ";
            }   
    }

        // connect
        mysql_connect("josetogbecom.fatcowmysql.com","username","password");
        mysql_select_db("gff_hff6a144eg");

        $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 Search Results Were Found for \"<b>$k<b>\"";
        }


        // disconnect
        mysql_close();

    ?>

Thanks in advance!

2 个答案:

答案 0 :(得分:1)

$i循环

之前检查$i=0计数器var是否设置为foreach

答案 1 :(得分:1)

此行的错误:

echo "No Search Results Were Found for \"<b>$k<b>/"";

应该是:

echo "No Search Results Were Found for \"<b>$k<b>\"";

此行的错误:

mysql close();

应该是:

mysql_close();

此行的错误:

$i++;

您应该始终初始化变量,如下所示:

$i = 0;
foreach ($terms as $each){
    $i++;

    if ($i == 1)
        $query .= "keywords LIKE '$each%' ";
    else
        $query .= "OR keywords LIKE '$each%' ";
} 

如果在纠正这两个错误后脚本仍然无效,请在脚本的开头添加这两行并告诉我们您的看法:

error_reporting(E_ALL);
ini_set('display_errors', 1);