2 Replies - 374 Views - Last Post: 04 January 2013 - 02:03 AM Rate Topic: -----

#1 curgan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-August 12

PHP Search Engine Problem

Posted 03 January 2013 - 12:02 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Search Engine Problem

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,235
  • Joined: 08-June 10

Re: PHP Search Engine Problem

Posted 03 January 2013 - 12:11 PM

View Postcurgan, on 03 January 2013 - 08:02 PM, said:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www...

your query fails, is as simple as that.

hint: print out the query.
Was This Post Helpful? 0
  • +
  • -

#3 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: PHP Search Engine Problem

Posted 04 January 2013 - 02:03 AM

in addition: don't use that mysql_* old crap! Use prepared statements --> PDO is the way to go!

EDIT:
very helpful tutorial, if you don't know PDO
http://www.dreaminco...1&#entry1712667

This post has been edited by Anthonidas: 04 January 2013 - 02:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1