Studio64
02-05-2003, 01:52 PM
I have a database of product's with descriptions and I'd like to be able to search the database from a user input (i.e. generic search box).
The code is fairly simple for a query with only one word in it.
SELECT * FROM `product` WHERE (`Descrip` LIKE '%$word%')
but, when the query is longer than one word, obviously the previous statement doesn't scale properly when more than one word is entered.
Is there a more appropriate query to use in this case?
ScottD
02-05-2003, 01:57 PM
SELECT * FROM product WHERE
descrip like '%word1%'
or descrip like '%word2%'
or descrip like '%word3%'ad naseum...
Zikkitzo
02-05-2003, 02:15 PM
Are you saying if they are using more then one word in the search?
Studio64
02-05-2003, 02:15 PM
So using eregi to break up the strings.
Now you have a essentially two searchs for two words without looking at the relation of the two words. Like does both word1 and word2 show up in this match?
Is there a more refined searching concept that would allow for better results?
Zikkitzo
02-05-2003, 02:22 PM
$Search = explode(" ", $Search); // Exploades by spaces
then in the search do
mysql_query("Select * From Search Where ". while($Search < $i){ print"table_to_search LIKE %$Search[]%"; } .";
If someone will clean up and or fix it since I don't know the whole while or way to repeat code to well :p
But the idea is sound, just explode by space then in the search have it add alot of blah AND blah AND so when it looks it looks for all those words...
Gyrbo
02-05-2003, 02:56 PM
Humm, your while code looks really, humm, funny. Try this:
$array = explode(' ', $what_the_user_entered);
$query = 'SELECT * FROM product WHERE ';
foreach($array as $in)
$query .= 'descrip LIKE "%'.$in.'%" OR ';
mysql_query($query.'0');
Untested, but it should work.
Zikkitzo
02-05-2003, 03:10 PM
Hey, I said the idea is sound. Not the code :p