Forum Moderators: coopster

Message Too Old, No Replies

need help with mysql query Keyword search

need help with mysql query Keyword search

         

BlackRaven

10:52 pm on Jul 25, 2004 (gmt 0)

10+ Year Member



yeah hi, ok here is my problem (having been trying to figure this out for 3 hours now) i want to do a key word search, how ever my sql query only returns words that have the word within them:
Example i search for food
what i get is "food and drink"
what i am missing is
"toad food and stuff" or "myfood"
the problem is the %food% operator that i am using only returns words that have a food characters inbetween them ignoring words that start or end with food.

this is what i am using:

if ( isset($search) && $searchfield!= "" )
{
$where_query .= empty($where_query)? " WHERE " : " AND ";
$where_query .= $searchfield." LIKE '%".$search."%'";
}

$query = "SELECT * FROM $table " . $where_query ." ORDER BY ". $orderby;

volatilegx

12:32 pm on Jul 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



maybe try:

$where_query .= $searchfield." REGEXP '".$search."'";

coopster

2:17 pm on Jul 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I can't see why your query wouldn't work as is, unless you need to trim the leading and trailing blanks from your $search variable, perhaps?

BlackRaven

4:35 pm on Jul 26, 2004 (gmt 0)

10+ Year Member



yes i did i used trim? would that cause a problem?

BlackRaven

2:27 am on Jul 27, 2004 (gmt 0)

10+ Year Member



after spending 6hours yester i ccould believe that a trim($search) could be such a PAIN! ALL newbies be aware.!

BlackRaven

2:28 am on Jul 27, 2004 (gmt 0)

10+ Year Member



thanks for pointing me into the right direction coopster

coopster

11:43 am on Jul 27, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You're welcome, but what exactly did you get stuck on? How were you using the trim() [php.net] function that made it difficult for you?