PHP Searching Multiple Fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
AmieCuteFeetandToes
Forum Newbie
Posts: 7
Joined: Tue Jan 26, 2010 6:07 pm
Location: Los Angeles, CA

PHP Searching Multiple Fields

Post by AmieCuteFeetandToes »

Hello Everyone!

I am working on a search form in PHP for a client's website. So everything works, the search brings results. What I am wanting to accomplish now is to be able to search more than one field, specifically first name and last name. Let's say if a user inputs let's say a first name AND a last name into the search input field and then searches, I want it to display results based on that. Right now if you try it, it says there are no results. My thought is how I've coded it is for the search box to be able to search only in ONE column field for per row.

Here is that part of my code:

Code: Select all

// QUERY THE DATABASE TABLE
$sql="SELECT * FROM contracts 
WHERE Status LIKE '%" . $searchTermDB . "%' 
OR DealerName LIKE '%" . $searchTermDB ."%' 
OR Dealer LIKE '%" . $searchTermDB ."%' 
OR Contract LIKE '%" . $searchTermDB ."%' 
OR LastName LIKE '%" . $searchTermDB ."%' 
OR FirstName LIKE '%" . $searchTermDB ."%' 
OR EffDate LIKE '%" . $searchTermDB ."%' 
OR TermDate LIKE '%" . $searchTermDB ."%' 
OR Year LIKE '%" . $searchTermDB ."%' 
OR Make LIKE '%" . $searchTermDB ."%' 
OR Model LIKE '%" . $searchTermDB ."%' 
OR Serial LIKE '%" . $searchTermDB ."%' 
OR TermMeter LIKE '%" . $searchTermDB ."%' 
OR Plan LIKE '%" . $searchTermDB ."%'";
Let me know of any suggestions. Thanks!

- Amie
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP Searching Multiple Fields

Post by califdon »

What that SQL statement does is search ALL of those fields to match THE SAME string ($SearchTermDB). Is that what you are trying to do? If you want to search for a first and last name and you have those 2 fields in your table and you have 2 <input> elements on your html form, it would look more like this:

Code: Select all

$sql="SELECT * FROM contracts
WHERE FirstName LIKE '%" . $firstname . "%'  AND LastName LIKE '%" . $lastname . "%'";
thamizh
Forum Newbie
Posts: 18
Joined: Wed Apr 14, 2010 7:25 am

Re: PHP Searching Multiple Fields

Post by thamizh »

for more than one field search result you can go for full text search in mysql
for further details
u can refer this webiste
http://dev.mysql.com/doc/refman/5.1/en/ ... earch.html
hope this helps u :)
Post Reply