Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LIKE '%$word%' question?

Status
Not open for further replies.

hisham

IS-IT--Management
Nov 6, 2000
194
I use the following query to search a table:
$query = mysql_query("SELECT * FROM my_table WHERE my_fild LIKE '%$word%'");
My_fild have the record: O & K , How can I modify the query to retrieve this record when user submit in the search box just: ok or o k
Thanks in advance
 
Before you pass $word to the mysql_query function, why don't you test to see if it is 'ok' or 'o k'. You could then modify it so that it's 'O & K'. You could it simply like this:

if(($word == 'ok') || ($word == 'o k')) {
$word = 'O & K';
}

Then, when $word is queried in mysql, it will match what you're looking for in the database. Does that help?

Matt
 
i can't do that, because there are many records in the database like this.
 
It is practically impossible to do searches for single letter combinations. These kinds of searches would bog down the system. You may want to create an additional field in your table for similar terms. For instance, 'Coca~Cola' may be the value in my_fild but a secondary search may be performed on 'coke' and 'cocacola'.

Here's a sample table:
ID|my_field|my_similar_field|
1|Coca~Cola|coke cocacola coca-cola|
2|Dr. Pepper|Doctor Pepper DoctorPepper DrPepper|

If the following SQL does not return results...
$query = mysql_query("SELECT * FROM my_table WHERE my_field LIKE '%$word%'");
...execute the following SQL...
$query = mysql_query("SELECT * FROM my_table WHERE my_similar_field LIKE '%$word%'");

The my_similar_field contents will require some creativity to imagine variations of the original contents of my_field. I log users' input on my search scripts so that I can better populate the my_similar_field contents.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top