Conditional Advanced Search Using PHP and MySql
September 18th, 2009No Comments
If you are working on an search page on a site where you have to search the keyword using the following conditions :
1. all the words
2. the exact phrase
3. at least one of the words
4. Without the words
Then you have to use MySQL FullText Search with the Boolean Full-Text Searches functionality to get the desired result.
$option = $_POST['option']; // condition
$keyword = $_POST['keyword']; // keyword
if ($option == "allwords") {
$result = mysql_query("SELECT * FROM db_table WHERE MATCH (field) AGAINST ('{$keyword}' IN BOOLEAN MODE)");
} elseif ($option == "exactphrase") {
$result = mysql_query("SELECT * FROM db_table WHERE MATCH (field) AGAINST ('\"{$keyword}\"' IN BOOLEAN MODE)");
} elseif ($option == "anywords") {
$result = mysql_query("SELECT * FROM db_table WHERE MATCH (field) AGAINST ('{$keyword}' IN BOOLEAN MODE)");
} elseif ($option == "withouttheword") {
$result = mysql_query("SELECT * FROM db_table WHERE MATCH (field) AGAINST ('-{$keyword}' IN BOOLEAN MODE)");
}
?>
