Forum Moderators: coopster

Message Too Old, No Replies

MySQL Full-text Searching in boolean mode

boolean operators in input corrupt mysql syntax

         

wasproject

3:33 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



Hi.

I have implemented MySQL Full-text Searching in boolean mode.

To retrieve the searchstring I use


$search = trim(mysql_escape_string($HTTP_GET_VARS['search']));

To execute the search I use


function getProductsbySearch($search){
$rs = mysql_query("SELECT *, MATCH(name, description) AGAINST ('$search') AS score FROM tbgameseek WHERE MATCH(name, description) AGAINST ('$search' IN BOOLEAN MODE) ORDER BY score DESC") or die(mysql_error());
return $rs;
}

To highlight the searchstring in the results I use


$row['description']=eregi_replace(($search), '<b>'.strtoupper($search).'</b>', $row['description']);

Highlighting works fine but
(1)only for single word searches without boolean operators.
At present I mention this in the FAQs of the site under construction, but I will be much obliged to anyone who has a more elegant solution.

(2)not when an ignorant user, no offence intended, searches for something like

+command +star
which will produce
Warning: eregi_replace(): REG_BADRPT in c:\easyphp1-8
for every result on the body of my page.
I could hack something to make this warning invisible, but for now, I find that not acceptable. My other still not very elegant solution would be use appropriate string methods/functions to detect and replace the offending boolean operators (so far, only
+
and
(
have this detrimental effect) before using the
eregi_replace
method.

I suspect that there is an elegant way of solving both problems in one go, for I have seen searches on shopping sites that do exactly what I desire, even highlighting multiple words of a default search in boolean mode.

I am looking forward to receive all well meant reactions.

jatar_k

2:52 pm on Aug 2, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you strip the bad chars, explode the search string on space and foreach the eregi. That should get both of your problems.

wasproject

3:13 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



Cheers jatar k.

I suspect that your suggestions will help me attain bliss if not perfection.

Much obliged.