Page 1 of 1

PHP Searching Multiple Fields

Posted: Wed Apr 21, 2010 6:07 pm
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

Re: PHP Searching Multiple Fields

Posted: Thu Apr 22, 2010 12:06 am
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 . "%'";

Re: PHP Searching Multiple Fields

Posted: Thu Apr 22, 2010 12:21 am
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 :)