Reputation: 69
I have created a php search engine by using an open source code. I have created a database and a table with 5 columns:
title, description, keywords, link, date
The code I'm using is the php code below:
<?php
$keywords = $_GET ['input'];
$keywords = "Led Zeppelin";
$words = explode(" ",$keyword);
foreach ($words as $w) {
$keyword_parts .= "+" . $w ." ";
}
$keyword_parts = substr($keyword_parts,0,-1);
$query = "SELECT
*,
MATCH (keywords,title) AGAINST ('" . $keywords . "') as score
FROM search
WHERE MATCH (keywords,title) AGAINST ('" . $keyword_parts . "' IN BOOLEAN MODE)
ORDER BY score DESC";
//connect
mysql_connect ("localhost", "root", "password");
mysql_select_db("intranet");
$query = mysql_query($query);
$numrows = mysql_num_rows ($query);
if($numrows >0){
while ($row = mysql_fetch_assoc($query)){
$id = $row ['id'];
$title = $row ['title'];
$description = $row ['description'];
$keywords = $row ['keywords'];
$link = $row ['link'];
$date = $row ['date'];
echo "<h2><a href='$link'>$title</a><h2/>
$description <br /><br />";
}
}
else
echo "No results found for \"<b>$each</b>\" ";
//disconnect
mysql_close();
?>
The search script works fine, but the only problem is that it searches by keyword which increases the search result, but makes it hard to find what you want because a lot of keywords on different entries match.
Now, I was reading online about full text search ("Full-text searching is performed using MATCH() ... AGAINST syntax. MATCH()") but I don't know how to apply that to my code.
Upvotes: 0
Views: 3469
Reputation: 1860
This is what I've used in the past. Also, the column you're doing a MATCH on must be altered to allow FULLTEXT:
$keyword = "Led Zeppelin";
$words = explode(" ",$keyword);
$keyword_parts = '';
foreach ($words as $w) {
$keyword_parts .= "+" . $w ." ";
}
$keyword_parts = substr($keyword_parts,0,-1);
$query = "SELECT
*,
MATCH (column1,column2) AGAINST ('" . $keyword . "') as score
FROM table
WHERE MATCH (column1,column2) AGAINST ('" . $keyword_parts . "' IN BOOLEAN MODE)
ORDER BY score DESC";
Upvotes: 2