?
Solved

Search function not displaying results for both names

Posted on 2013-06-19
3
Medium Priority
?
253 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
[X]
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
3 Comments
 
LVL 7

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 2000 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 111

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 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