I m having this problem while executing search

Notice: Undefined variable: i in D:\xampp\htdocs\search.php on line 23

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\search.php on line 35

.

here is my source code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search engine</title>
</head>

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

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

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

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

 // connect
 mysql_connect("localhost","root");
 mysql_select_db("search");

 $query=mysql_query($query);
 $numrows=mysql_num_rows($query);
 if($numrows>0){
 while($row=mysql_fetch_assoc($query)){
 $id=$row['id'];
 $id=$row['title'];
 $id=$row['description'];
 $id=$row['link'];

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

 }
 }

 else
 echo"NO results found for \<b>$k</b>\"";


 // disconnect
 mysql_close();


?>



</center>
</body>

</html>

please help me debugging this one

Where you have //connect place print_r($query); exit; and check if the query has a regular structure, probably there is something wrong with the loop above, I don't see any space beetween WHERE and conditions.

good catch..

also looks like there might be too many closing quotes on '%$each%''"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.