Page 1 of 1

Search Problem

Posted: Wed Sep 21, 2005 6:28 pm
by alexmaster_2004
Hi
i implement a search page in my site.
i use the search page to search mysql db for products.
now i can enter the name of the product and then go to
the db and search for this product by name.
the problem is :
suppose that i have a product that its name is "Linux".
then if i write in the search box "Linux" then the query
return that product.
but what about if i write in the search box "Linu" OR "Lin"
now the query don't return any thing.
as my code is:

Code: Select all

$name = $HTTP_POST_VARS["product_name"];
$res = mysql_query("select * from products where productName like '$name' ");
$rows = mysql_num_rows($res);
if($rows != 0)
{
print "PRODUCT HAS FOUND";
}
else
{
print "PRODUCT HAS NOT FOUND";
}
i want to know how i could make the query return
the product "Linux" although i may enter "Lin" or any
similar string
Thanks in advance

Posted: Wed Sep 21, 2005 6:31 pm
by John Cartwright

Code: Select all

$res = mysql_query("SELECT * FROM `products` WHERE `productName` LIKE '%$name%' ");

Posted: Wed Sep 21, 2005 6:44 pm
by alexmaster_2004
Hi
i tried it but it get error.
"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource"

Posted: Wed Sep 21, 2005 6:47 pm
by feyd
you likely have an error in your syntax or something else...

helping hint:

Code: Select all

$result = mysql_query('SELECT * FROM foo') or die(mysql_error());

Posted: Wed Sep 21, 2005 6:51 pm
by alexmaster_2004
Hi
you are right.
but what about if i want to use just say 3 chars of the product name
ex: if i enter in the search box "Linux System"
i want just search by "Lin"
Thanks

Posted: Wed Sep 21, 2005 7:00 pm
by feyd
I'm not so sure of what you are asking...

Posted: Wed Sep 21, 2005 8:07 pm
by alexmaster_2004
hi
ok i will try to explain:
i ask what about if i enter in the search box "WinXP"
how i force this search term to match with the product name "Win"
this code is not valid

Code: Select all

$res = mysql_query("SELECT * FROM `products` WHERE `productName` LIKE '%$name%' ");
it didn't return any matching.
all what i want is make it possible to get the product name "Win"
when i enter in the search "WinXP" OR "Win2000".
Thanks

Posted: Wed Sep 21, 2005 8:17 pm
by feyd

Posted: Wed Sep 21, 2005 9:05 pm
by John Cartwright
The thing is if your going to match anything against any part the word, your going to get some pretty irrelevant matches. :?

Posted: Thu Sep 22, 2005 6:36 am
by alexmaster_2004
Hi
i'm not going to match anything against any part of the word.
but i want to make the search intelligent enough to know that
"WinXP" can match with "Win".
As i see substr() will not help me!!!!!.because i don't know
what the user will enter for search.
let's make more complex:
Assume i have a product "Real Player" in the Db.
then the user will search for "RealPlayer" how this will match???
also what about "Real Music" is this will match and how???
the problem that my DB is have a lot of products.
the user will search for some product by its name.
of-course there will be a case that the user misspell
the product name like enetr "Rael Player" then i want
to know is it possible to make them match.
i have found 2 functions soundex() and metaphone() that i think they may be useful but i don't know how????
If any ine can help me i will be pleased
Thanks

Posted: Thu Sep 22, 2005 10:08 am
by feyd
Smee suggests using the MySQL functions for those, since it they are native to it, yar.