Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

PHP Search Engine Problem

$
0
0
Hello there, I was just working on creating a php search engine. Unfortunately, its not been working well as expected. Tried detecting the error but to no avail. Please can some go through my code and tell me what i've done wrong?
<?php
	//connect
	mysql_connect("localhost", "user", "pass");
	mysql_select_db("database");
	$query = mysql_query("SELECT * FROM search");
	
	if (isset($_GET['keyword'])&& $_GET['keyword'] != "") {
	$keyword = mysql_real_escape_string(htmlentities($_GET['keyword']));
	echo "Result";
	//explode the search term
	$terms = explode(" ",$keyword);
	
	foreach($terms as $each) {
	$i++;
		if($i==1)
			$query .= "keywords LIKE '%$each%' ";
		else
			$query .= "OR keywords LIKE '%$each%' ";
	}
	
	//$construct ="SELECT * FROM search WHERE $construct";
	$run = mysql_query($query);
	$numrows = mysql_num_rows($run);
	
	if ($numrows==0) {
		echo "Sorry, no match found for <b>$keyword</b>.</br></br>";
	} else{
		echo "$numrows results found !<p>";
		
		while($numrows = mysql_fetch_assoc($run)){
			$title = $numrows ['title'];
			$description = $numrows ['description'];
			$link = $numrows ['link'];
			echo "
			<div class='width: 400px;'>
			<div class='title'><a href='$link'><b>$title</b></a></div>
			<div class='link'>$link</div>
			<div class='desc'>$description</div>
			</div>
			<br />
			";
		}
	  }
	}
	else
	{
	echo "Search field is empty!";	
	}


This is the error message I've been getting; Notice: Undefined variable: i in C:\wamp\www...

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www...
NOTE: title, description and link are from my database.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>