rantum Posted April 7, 2011 Share Posted April 7, 2011 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" if($results != 0) even though it has been defined... not really sure what to do about this. Here is the 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>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> Any help would be much appreciated! Thanks, rantum Quote Link to comment https://forums.phpfreaks.com/topic/232958-search-engine-help/ Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 the variable is only defined if this statement is true, i think if($results = mysql_num_rows($sql_query) != 0) and im a bit confused by that statement in itself anyways, what are you trying to do here exactly? Quote Link to comment https://forums.phpfreaks.com/topic/232958-search-engine-help/#findComment-1198150 Share on other sites More sharing options...
rantum Posted April 7, 2011 Author Share Posted April 7, 2011 If I'm honest it isn't my code, just a free script I found which seems like if it works would be perfect for my website. Basicially just a search box which when you search for certain key words it brings up results (if you know of where I can get a script which works or a guide to making one then that would be great!) Anyway I'm a beginner at PHP but from what I see the if statement will display a table with the results if any results are found. Quote Link to comment https://forums.phpfreaks.com/topic/232958-search-engine-help/#findComment-1198157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.