为什么我会收到PHP语法错误

时间:2015-04-22 00:46:59

标签: php

我正在制作搜索引擎,但收到错误,似乎无法找出原因。请看一下,让我知道我搞砸了哪里。 eI说这个错误出现在" foreach"部分我尝试了一切,但没有解决问题。任何帮助深表感谢。

结果页面

<?php
        $input = $_GET['input'];
        $terms = explode(" ", $input);
        $query = "SELECT * FROM search WHERE ";

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

        // connecting to our mysql database
        mysql_connect("localhost", "root", "");
        mysql_select_db("test");

        $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 results found for \"<b>$input</b>\"";

        // disconnect
        mysql_close();
    ?>

1 个答案:

答案 0 :(得分:1)

我相信您的条件括号已关闭。

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

需要

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