PHP搜索框无法正常工作?

时间:2016-09-13 10:03:53

标签: php

我创建了一个搜索我的网站的搜索框。我已经尝试过很难修复它,但它没有用。你能帮忙吗?代码如下:

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

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

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

   $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 "<h1><a href='$link'>$title</a></h2>";
           $description<br  /><br />""
       }
   }
   else
       echo "No results found for \"<b>$k/b>\" ";
   //disconnect
   mysql_close();
  ?>

1 个答案:

答案 0 :(得分:0)

首先,正确的语法。而不是:

$query = "SELECT * FROM search WHERE " 
for each ($terms as $each) {

应该是

$query = "SELECT * FROM search WHERE ";  
foreach ($terms as $each) {

和OUH,这是什么???

$description<br  /><br />""

也许你最初会学习语法的基础知识。