Hi ,
I am working on full-text boolean search.I have coded the program but I get an error :
You have an error in your SQL syntax near 'boolean mode)' at line 1
Here's my code:
<html>
<head><title>Search</title></head>
<body>
<?php
$cnx = mysql_connect('localhost', 'user', 'password') or die ("Could not connect");
mysql_select_db('db', $cnx) or die (mysql_error());
function searchForm()
{
$searchwords = (isset($GET['words']) ? htmlspecialchars(stripslashes($REQUEST['words'])) : '');
$normal = (($GET['mode'] == 'normal') ? ' selected="selected"' : '' );
$boolean = (($GET['mode'] == 'boolean') ? ' selected="selected"' : '' );
echo '<form method="get" action="'.$_SERVER['PHP_SELF'].'">';
echo '<input type="hidden" name="cmd" value="search" />';
echo 'Search for: <input type="text" name="words" value="'.$searchwords.'" /> ';
echo 'Mode: ';
echo '<select name="mode">';
echo '<option value="normal"'.$normal.'>Normal</option>';
echo '<option value="boolean"'.$boolean.'>Boolean</option>';
echo '</select> ';
echo '<input type="submit" value="Search" />';
echo '</form>';
}
// Create the navigation switch
$cmd = (isset($GET['cmd']) ? $GET['cmd'] : '');
switch($cmd)
{
default:
echo '<h1>Search Database!</h1>';
searchForm();
break;
case "search":
searchForm();
echo '<h3>Search Results:</h3><br />';
$searchstring = mysql_escape_string($_GET['words']);
switch($_GET['mode'])
{
case "normal":
$sql = "SELECT id,author,title,year,MATCH(author,title) AGAINST ('$searchstring') AS score FROM pubs WHERE MATCH(title) AGAINST ('$searchstring')";
break;
case "boolean":
$sql = "SELECT * FROM pubs WHERE MATCH(title) AGAINST('$searchstring' IN BOOLEAN MODE) AS score FROM pubs WHERE MATCH(title) AGAINST ('$searchstring' IN BOOLEAN MODE)";
break;
}
$result = mysql_query($sql) or die (mysql_error());
while($row = mysql_fetch_object($result))
{
echo '<strong>Title: '.stripslashes(htmlspecialchars($row->title)).'</strong><br />';
echo '<strong>Author: '.stripslashes(htmlspecialchars($row->author)).'</strong><br />';
echo '<hr size="1" />';
}
break;
}
?>
</body>
</html>
I am not able to figure the problem.Is it with the coding?I have also created a full text index:
CREATE FULLTEXT INDEX full_index ON pubs(author,title,year);
Please help me out.