Reply
How to search SQL db
Old 12-15-2007, 05:41 PM How to search SQL db
Extreme Talker

Posts: 165
I posted this question a qhile back in the PHP boards but I didnt get a workable response so I figured I would give it a better shot, hopefully I can give a better explanation of my problem.

I am trying to make a search function using php that searches through one column of a db to find a match. I have it working fine so far but only if the user spells the name correctly.

Now I have it set both terms to uppercase and I tried %LIKE% but there must be a more effective way. Like how to most big sites search through databases, or even the webmaster-talk forums (im assuming VBulletin). On the php forums they suggested using phonetics but Im just thinking that there must be something else out there.

Searching on google I havent been able to find anything of substance or any premade scripts.

Any suggestions?
Truly is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 12-15-2007, 07:29 PM Re: How to search SQL db
chrishirst's Avatar
Super Moderator

Posts: 11,894
Location: Blackpool. UK
assuming MySql;

Maybe use SOUNDEX()

Just about every search I have ever developed has required that the user can spell correctly though.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-15-2007, 07:31 PM Re: How to search SQL db
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,025
Name: Forrest Croce
Location: Seattle, WA
Quote:
Originally Posted by Truly View Post
Now I have it set both terms to uppercase and I tried %LIKE% but there must be a more effective way. Like how to most big sites search through databases, or even the webmaster-talk forums (im assuming VBulletin). On the php forums they suggested using phonetics but Im just thinking that there must be something else out there.
Other than near misses ... near vs neer ... what's wrong with the like operator?

It's really a very large topic, how to build a spell check into a search function, try to pull semantic meaning out of the search phrase and the documents in the database, or to try and 'teach' or code a phonetic read. That's probably why you haven't found anything all that helpful ... I don't think you'll find anything concrete, just a few disparate theories to put together.

One thing Google does is, unless you use quotes around a search, break your phrase up into individual words. Search for rabbit food; the #1 result uses both words, but not together.

Most search engines - Endecca, FAST, etc - use a technique called stemming. The word ran could match run, but not errand. Walk could be walking, walker, walks, and so on.

Then you've got abbreviations, which are usually domain specific. A financial search is more likely to match CO to company than Colorado, for example.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 12-15-2007, 09:10 PM Re: How to search SQL db
Extreme Talker

Posts: 165
hmmm ok I guess for now I will just assume people can spell and maybe do the breaking up of search terms with seperate searches later with soundex.

I think stemming is a little complicated for me . thanks guys
Truly is offline
Reply With Quote
View Public Profile
 
Old 12-17-2007, 06:27 AM Re: How to search SQL db
Experienced Talker

Posts: 30
You can use "LIKE" in your query...
If you for example want the user to enter a keyword to search a text (with is stored in database) you can use LIKE like this:
PHP Code:
$Find mysql_query("select * from Texts where text LIKE '%$keyword%' "); 
So it will search for texts wich contain the keyword.
You can also use like this:
PHP Code:
// Starts with keyword
$Find mysql_query("select * from Texts where text LIKE '$sv%'");
// End with keyword
$Find mysql_query("select * from Texts where text LIKE '%$sv'");
// And the contains wich is given in the first window 
klaroen is offline
Reply With Quote
View Public Profile
 
Old 12-18-2007, 09:02 PM Re: How to search SQL db
Extreme Talker

Posts: 165
I guess I just never thought about this before, I always assumed there was a premade function that made it easy to search, didnt realize how inefficient it was. You would think that it would be something pre-designed. Maybe its too application specific for the designers of PHP to do that, or maybe thats just down the road.

Any ideas?
Truly is offline
Reply With Quote
View Public Profile
 
Old 12-23-2007, 05:47 AM Re: How to search SQL db
Experienced Talker

Posts: 30
Hmm, they can't really integrate a search function, because what should it return?
PHP doesn't know wich tables you have and rows in them...

The best way is to make a search page, with a form where they need to enter a word or something, and let php search the database as I said in my last post. Then with a simple while lus you can give back all the database elements containing that word.
klaroen is offline
Reply With Quote
View Public Profile
 
Old 12-24-2007, 04:02 AM Re: How to search SQL db
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,025
Name: Forrest Croce
Location: Seattle, WA
Quote:
Originally Posted by Truly View Post
I guess I just never thought about this before, I always assumed there was a premade function that made it easy to search, didnt realize how inefficient it was. You would think that it would be something pre-designed. Maybe its too application specific for the designers of PHP to do that, or maybe thats just down the road.

Any ideas?
Soundex is probably as close as it comes. Part is different words on a photo blog vs a news site... But it would be a great market...
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 12-29-2007, 12:44 AM Re: How to search SQL db
Average Talker

Posts: 24
Name: Nick Cousins
Location: Northern Ireland
I'm working on a site that is entirely based on search.

The main advantage I have is that it is a search for businesses, so most search phrases are going to be either names or types of business - which narrows the field a little.

My search algo is pretty complicated all the same, taking into consideration things like the fact that many words which end in "s" are plural, and may have a singular match in the DB.

So the business may be a Butcher, and the user searches for "Butchers" - to get around this *one* of my techniques is to drop any trailing "s"es and replace them with a wildcard.

Another idea is that mis-spellings such as Macro instead of Makro (the wholesaler) would yield no results - so my algo uses the levenshtein function to compare each word in a search query against all of the words in the index of business names (without duplicates) - to find one which is closest to the user's apparent mis-spelling.

This has it's drawbacks, depending on the size of the word-index - but can be assisted by using SOUNDEX to check that the words also sound the same.

This is the most accurate way I have found to counter mis-spellings without writing vast intellegent stemming and context-reading scripts, although it is a Huuuuuuuge subject!
HandCoder is offline
Reply With Quote
View Public Profile Visit HandCoder's homepage!
 
Old 01-01-2008, 07:33 PM Re: How to search SQL db
Extreme Talker

Posts: 165
Thanks guys. My head is filled with ideas now, when I get a few things done on what Im working on right now im definetly going to dabble :P
Truly is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to search SQL db
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15865 seconds with 13 queries