Search Engine Help
Posted: Thu Apr 07, 2011 5:41 am
I'm trying to create a search engine for my website and I found some code which claims to do this, I've set up all of the tables etc. in MySQL and am just running it on localhost at the moment (using WAMP).
However I've come accross a problem, I'm getting an error which reads "Undefined variable: results on line 53"
even though it has been defined... not really sure what to do about this.
Here is the code:
Any help would be much appreciated!
Thanks,
rantum
However I've come accross a problem, I'm getting an error which reads "Undefined variable: results on line 53"
Code: Select all
if($results != 0)Here is the code:
Code: Select all
<!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>Untitled Document</title>
</head>
<body>
<h1>Search Engine with PHP</h1>
<form method="POST" action="">
<input type="text" name="search_term" title="Search…" value="">
<input type="submit" name="search" title="Search Now!
"value="Search" class="searchbutton" />
</form>
<?PHP
function getmicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$connection_string = dirname(__FILE__) . "/connectionstring.php";
require_once($connection_string);
mysql_select_db("test") or die ( 'Unable to select database.' );
$RESULTS_LIMIT=10;
if(isset($_GET['search_term']) && isset($_GET['search_button']))
{
$search_term = $_GET['search_term'];
if(!isset($first_pos))
{
$first_pos = "0";
}
$start_search = getmicrotime();
$sql_query = mysql_query("SELECT * FROM news WHERE MATCH(title,article) AGAINST('$search_term')");
//many mathces (too many matches cause returning of 0 results)
if($results = mysql_num_rows($sql_query) != 0)
{
$sql = "SELECT * FROM news WHERE MATCH(title,article) AGAINST('$search_term') LIMIT $first_pos, $RESULTS_LIMIT";
$sql_result_query = mysql_query($sql);
}
else
{
$sql = "SELECT * FROM news WHERE (title LIKE '%".mysql_real_escape_string($search_term)."%' OR article LIKE '%".$search_term."%') ";
$sql_query = mysql_query($sql);
$results = mysql_num_rows($sql_query);
$sql_result_query = mysql_query("SELECT * FROM news WHERE (title LIKE '%".$search_term."%' OR article LIKE '%".$search_term."%') LIMIT $first_pos, $RESULTS_LIMIT ");
}
$stop_search = getmicrotime();
$time_search = ($stop_search - $start_search);
}
?>
<?PHP
if($results != 0)
{
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td width="47%">Results for <?PHP echo "<i><b><font color=#000000>".$search_term."</font></b></i> "; ?></td>
<td width="53%" align="right" height="22">Results <b>
<?PHP echo ($first_pos+1)." - ";
if(($RESULTS_LIMIT + $first_pos) < $results) echo ($RESULTS_LIMIT + $first_pos);
else echo $results ; ?>
</b>
out of <b><?PHP echo $results; ?></b>
for(<b><?PHP echo sprintf("%01.2f", $time_search); ?></b>)
seconds </td>
</tr>
<tr>
<form action="" method="GET">
<td colspan="2" align="center"> <input name="search_term" type="text" value="<?PHP echo $search_term; ?>" size="40">
<input name="search_button" type="submit" value="Search"> </td>
</form>
</tr>
<?PHP
while($row = mysql_fetch_array($sql_result_query))
{
?>
<tr align="left">
<td colspan="2"><?PHP echo $row['title']; ?></td>
</tr>
<?PHP
}
?>
</table>
<?PHP
}
elseif($sql_query)
{
?>
<table border="0" cellspacing="2" cellpadding="0">
<tr>
<td align="center">No results for <?PHP echo "<i><b><font color=#000000>".$search_term."</font></b></i> "; ?></td>
</tr>
<tr>
<form action="" method="GET">
<td colspan="2" align="center">
<input name="search_term" type="text" value="<?PHP echo $search_term; ?>">
<input name="search_button" type="submit" value="Search">
</td>
</form>
</tr>
</table>
<?PHP
}
?>
</body>
</html>Thanks,
rantum
