搜索表单错误

时间:2014-03-11 06:41:34

标签: php

美好的一天eveyone!

我正在尝试制作一个简单的搜索引擎。在我的数据库中,我为关键字(键)放置了一列,这样每当我尝试搜索关键字上具有相同单词的内容时,它就会显示其结果。但为什么代码不起作用。它不会返回任何结果,也会出错:

  

mysql_num_rows()期望参数1为资源,布尔值为

我需要你的帮助。

的search.php

<html>
<body>
    <form action='search.php' method='get'>
        <input type='text' name='k' size='50' value='<?php echo $_GET['k']; ?>' />
        <input type='submit' value='search'>    
    </form>

<?php
    $k = $_GET['k'];
    $terms = explode(" ", $k);
    $q = "SELECT * FROM book_info WHERE ";
    $i = 0;

    foreach ($terms as $each)
    {
        $i++;
        if($i == 1)
            $q .= "key LIKE '%$each%'";
        else 
            $q .= "OR key LIKE '%$each%'";
    }

    mysql_connect("localhost", "root", "");
    mysql_select_db("library_books");

    $q = mysql_query($q);
    $numrows = mysql_num_rows($q);

    if($numrows >0)
    {
        while($row = mysql_fetch_assoc($q)){
            $title = $row['title'];
            $author= $row['author'];
            $isbn= $row['isbn'];

        echo "$title<br /> $author<br />$isbn <br />";

        }

    }else
        echo "No results found for \"<b>$k</b>\"";
    mysql_close();
?>
</body>
</html>

的index.php

<html>
<body>
    <form action='search.php' method='get'>
        <input type='text' name='k' size='50' />
        <input type='submit' value='search'>

    </form>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

试试这个:

<?php
    $k = isset($_GET['k']) ? $_GET['k'] : 'no result';
    $terms = explode(" ", $k);
    $q = "SELECT * FROM book_info WHERE ";
    $i = 0;

    foreach ($terms as $each)
    {
        $i++;
        if($i == 1)
            $q .= "book_info.key LIKE '%$each%'";
        else 
            $q .= " OR book_info.key LIKE '%$each%'";
    }



    $link = mysql_connect("localhost", "root", "");
    mysql_select_db("library_books", $link);

    $result = mysql_query($q, $link);
    $numrows = mysql_num_rows($result);

    if($numrows >0)
    {
        while($row = mysql_fetch_assoc($result)){
            $title = $row['title'];
            $author= $row['author'];
            $isbn= $row['isbn'];

        echo "$title<br /> $author<br />$isbn <br />";

        }

    }else
        echo "No results found for \"<b>$k</b>\"";
    mysql_close();
?>
<html>
<body>
    <form action='search.php' method='get'>
        <input type='text' name='k' size='50' value='<?php echo $k; ?>' />
        <input type='submit' value='search'>    
    </form>
</body>
</html>

答案 1 :(得分:0)

Foreach循环有错误。如果你有多行,你的sql将像“WHERE OR ..”一样开始,但情况并非如此。使用以下代码进行复制。

$mutiple = false;
foreach ($terms as $each)
{
    $i++;
    if(sizeof($terms) == 1) {
        $q .= "`key` LIKE '%$each%'";
    }
    else {  
        if($multiple) $q .= " OR ";
        $q .= "`key` LIKE '%$each%'";
        $mutiple = true;
    }
}

同时更改

 $terms = explode(" ", $k);

$terms = array_map('trim', explode(' ', $k));

答案 2 :(得分:0)

你可以尝试:

$q = mysql_query($q); alert($q); $numrows = mysql_num_rows($q);

检查您的数据库是否实际返回了某些内容。