Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Ignoring white space on MySQL LIKE statement

Posted on 2007-03-21
6
445 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 143

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

829 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