Solved

Ignoring white space on MySQL LIKE statement

Posted on 2007-03-21
6
435 Views
Last Modified: 2013-12-13
HI guys,

I have a search page feature on my website (PHP & MySQL 5). At the moment i am using SELECT * FROM table WHERE somefield LIKE '%keywords%'

The problem is the search became really sensitive when it comes to whitespace. Say if i want to search for "Radio A", i have to put "Radio A" for the search to find the record.  When someone put "RadioA" (Without spaces) it wont find any results.

I dont want to explode "Radio A" into "Radio" and "A", as it will return any other "Radio" results which we dont want.

Any hints and tips would be appreciated.

0
Comment
Question by:JaZziD
6 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 18763230
SELECT * FROM table WHERE somefield LIKE '%keywords%' OR somefield LIKE '%trim(keywords)%'
0
 
LVL 48

Expert Comment

by:hernst42
ID: 18763290
regular expresion might help in thiscase (if your mysql-version supports it)
http://dev.mysql.com/doc/refman/5.0/en/regexp.html

in your programming langnguage use a function to replace any whitespace with an emoty string, then put between each char a \s*
so if the user enters "radioA"or "radio A" $keywords will be "r\s*a\*sd\s*i\s*o\s*A"
and the sql:

SELECT * FROM table WHERE somefield REGEXP 'r\s*a\*sd\s*i\s*o\s*A'
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 250 total points
ID: 18763298
Yup error in the example. you need to replace any occurency of \s with [[:space:]] in my example
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18763395
or, replace the whitespace by % before submitting the keyword for the search...
0
 

Author Comment

by:JaZziD
ID: 18770091
hi guys,

let me try what hernst suggested first
0
 

Author Comment

by:JaZziD
ID: 18770555
hi hernst... thanks for that, it works great.

angelll: when i tried to replace the whitespace with % it didnt work somehow
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now