PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
rantum
Forum Newbie
Posts: 3 Joined: Thu Apr 07, 2011 5:29 am
Post
by rantum » 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:
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>
Any help would be much appreciated!
Thanks,
rantum
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Thu Apr 07, 2011 11:33 am
Code: Select all
if($results = mysql_num_rows($sql_query) != 0)
// change to this
$sql_query = mysql_query("SELECT * FROM news WHERE MATCH(title,article) AGAINST('$search_term')");
$results = mysql_num_rows($sql_query);
if ($results != 0) {
// rest of your code
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
rantum
Forum Newbie
Posts: 3 Joined: Thu Apr 07, 2011 5:29 am
Post
by rantum » Thu Apr 07, 2011 12:38 pm
social_experiment wrote: Code: Select all
if($results = mysql_num_rows($sql_query) != 0)
// change to this
$sql_query = mysql_query("SELECT * FROM news WHERE MATCH(title,article) AGAINST('$search_term')");
$results = mysql_num_rows($sql_query);
if ($results != 0) {
// rest of your code
Thanks a lot for the reply but I'm still a bit confused. I understand that I change this
to this
Code: Select all
if($results = mysql_num_rows($sql_query) != 0)
but then where does the other piece of code you wrote go?
(sorry if it's really trivial but I am quite new to PHP coding)
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Thu Apr 07, 2011 1:35 pm
Code: Select all
$sql_query = mysql_query("SELECT * FROM news WHERE MATCH(title,article) AGAINST('$search_term')");
//many mathces (too many matches cause returning of 0 results)
// add change here
It goes below this section of code
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
rantum
Forum Newbie
Posts: 3 Joined: Thu Apr 07, 2011 5:29 am
Post
by rantum » Thu Apr 07, 2011 1:53 pm
I've made the changes you suggested but I'm still getting errors, this is what I see when I run the code:
Here is the ammended code (I think I've done what you said right but I may be wrong)
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')");
$results = mysql_num_rows($sql_query);
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 = mysql_num_rows($sql_query) != 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 again for replying, hopefully can get this sorted soon!
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Thu Apr 07, 2011 4:30 pm
Code: Select all
$results = mysql_num_rows($sql_query);
if($results != 0)
This line of code is part of the problem. You are assigning a variable and comparing it at the same time
Completely remove it, in both instances. And the second instance is moot as you are already testing for != 0, why check it again.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering