简体   繁体   English

为什么我的PHP搜索引擎找不到任何记录?

[英]Why can't my PHP search engine find any records?

I've written a piece of code but I can't seem to make it work. 我已经编写了一段代码,但似乎无法使其正常工作。 I do have a connection with my database. 我的数据库确实有连接。 When I submit the form it goes to the else block ( echo "Geen resultaat gevonden voor \\"<b>$s</b>\\""; ) 当我提交表单时,它会转到else块( echo "Geen resultaat gevonden voor \\"<b>$s</b>\\"";

What am I doing wrong with my code? 我的代码在做什么错? I've also added a screenshot of my database. 我还添加了数据库的屏幕快照。

<body>

        <h2> hier komt een kleine foto</h2>
        <form action='./search.php' method='get'>
            <input type='text' name='s'size='50' value='<?php echo $_GET['s']; ?>' />
            <input type='submit' value='Zoek'/>
        </form>
        <hr />
        <?php       
            $s = $_GET['s'];            
            $terms = explode (" ", $s);
            $query = "SELECT * FROM 'ID' WHERE ";

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

                if ($i ==1)
                    $query .= "keywords LIKE '%$each%' ";
                else
                    $query .= "OR keywords LIKE '%$each%' ";
            }
            //connect to database
            mysql_connect("server", "username", "password");
            mysql_select_db("database");

            $query = mysql_query($query);
            $numrows = mysql_num_rows($query);
            if(numrows > 0){

                while($row = mysql_fetch_assoc($query)){
                    $id = $row['id'];
                    $photo = $row['photo'];
                    $title = $row['title'];
                    $description = $row['description'];
                    $price = $row['price'];
                    $Link = $row['Link'];
                    $keywords = $row['keywords'];

                    echo  "<h2><a href='$Link'>$title</a></h2>
                    $description<br  /><br  />";
                }

            }
            else
                echo "Geen resultaat gevonden voor \"<b>$s</b>\"";

            //disconect
            mysql_close();
        ?>
</body>
</html>

在此处输入图片说明

You have a couple of issues. 您有几个问题。

  • if(numrows > 0){ should be looking at $numrows if(numrows > 0){应该查看$numrows
  • $query = "SELECT * FROM 'ID' WHERE " - You have wrapped your table name in single quotes. $query = "SELECT * FROM 'ID' WHERE " -您已将表名用单引号引起来。 This will cause a syntax error - consider changing to backticks (`), or removing the quotes completely. 这将导致语法错误-考虑更改为反引号(`),或完全删除引号。
  • You shouldn't use mysql_* functions - they are deprecated for a reason. 您不应该使用mysql_*函数-不推荐使用它们是有原因的。 The PHP manual suggests you use mysqli_* functions or PDO instead. PHP手册建议您改用mysqli_*函数或PDO。

The first one will fix your current problem, but the second one is far more important, and something you should look further in to. 第一个可以解决您当前的问题,但是第二个要重要得多,您应该进一步研究。

It looks like you're fairly new to PHP and MySQL - so this is a good opportunity for you to learn it correctly. 看来您是PHP和MySQL的新手,所以这是您正确学习它的好机会。

您好像缺少$之前numrows上这一行:

if(numrows > 0){

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

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