Solved

Search function not displaying results for both names

Posted on 2013-06-19
3
237 Views
Last Modified: 2013-06-19
I have a search function where you can search someones name. The first name and surname are in different database columns so i'm using this...

WHERE fname LIKE '%$search%' OR lname LIKE '%$search%'

It's not working though. Say for example 'John Smith' is one of the searchable names. If you search 'John' or 'Smith' he displays for if you search 'John Smith' he doesn't.

Any idea how I can get this too work?
0
Comment
Question by:BrighteyesDesign
3 Comments
 
LVL 6

Expert Comment

by:Robert Saylor
ID: 39260115
what you have to do is 1st remove the space from your search string then concat the two fields and do a like search.
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39260117
Split the name so you can add them separately to the sql
Your current sql requires the column/s contain john smith exactly in either column (and ignore anything before or after the term in the column.)
Or use CONCAT
WHERE CONCAT(fname,' ',lname) LIKE '%$search%'
Though you probably don't want the wild card for this method unless there are other names, e.g. middle names
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39260151
Another possibility...

WHERE fname LIKE '%$search%' OR lname LIKE '%$search%' OR CONCAT(fname,' ',lname) LIKE '%$search%'
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

747 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

13 Experts available now in Live!

Get 1:1 Help Now