| JUMP TO: |
> > Searching the Table |
Quick and Dirty Search Engine with PHP and MySQL
By Clay Johnson
April 30, 1999
Searching the Table
Now you have an easy to-use table of keywords and their associations. How do you query this table? Here's what I do:
First I format each searchterms passed into the script as 'word1','word2','word3' and stick it in a string called $querywords.
Then I throw them into this SQL query:
SELECT count(search_table.word) as score, search_table.qid,your_table.blob
FROM search_table,your_table
WHERE your_table.qid = search_table.qid AND search_table.word
IN($querywords)
GROUP BY search_table.qid
ORDER BY score DESC";
Set that query to $search, and print out the results like so:
<?
$getresults = mysql_query($search);
$resultsnumber = mysql_numrows($getresults);
IF ($resultsnumber == 0){
PRINT "Your search returned no results. Try other keyword(s).";
}
ELSEIF ($resultsnumber > 0){
PRINT "Your search returned $resultsnumber results<BR>Listing them
in order of relevance<BR><BR>";
for($count = 0; $count<$resultsnumber; $count++){
$body = mysql_result($getresults,$count,"blob");
$qid = mysql_result($getresults,$count,"qid");
//tighten up the results
$body2print = substr($body, 0, 100);
$cnote = $count+1;
PRINT "$cnote. <a href=/old?u=http%3A%2F%2Fwww.devshed.com%2FServer_Side%2FPHP%2FSearch_Engine%2Fyourcontent.php3%3Fqid%3D%24qid%26gt%3B&y=1999
<i>$body2print...</i></a><BR>";
}
}
?>
Presto, you've got keyword searching for your database, complete with relevancy ranking. It may not be Google or altavista.
It may not support all those fancy boolean operators, or excite's (*cough*) conceptual mapping technology. But it works, its quick and enough to handle your user's demand.
Click here to receive updates on the latest articles.
|
 |
Now Available:
 Click to Order!Search for books on PHP
Developer News:
Hot Forum Topics
|