Solved

Search function not displaying results for both names

Posted on 2013-06-19
3
239 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WordPress TK Title 8 25
php string masking 4 29
array_values - reorder after unset? 5 14
hosting images 4 12
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 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 …

863 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