Connecting Tech Pros Worldwide Help | Site Map

search data using more than one search fields

Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#1: Sep 25 '09
I have a php search page which is having more than one search felds. This is my query
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM vehicles WHERE VehicleType='$type' AND VehicleMake='$make' AND VehicleModel='$model' AND Year= '$year' AND Price BETWEEN '$minPrice' AND '$maxPrice'
  2.  
This will get results only if all fields match. But I want to get results if only one field match. How can I do that?
ex: If user enter only vehicle make and price I want to display data by searching db only the entered fields.
best answer - posted by kkshansid
Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT * FROM table where f1 = '$f1'";/*u have to make f1 necessary to fill*/
  2. if(is_string($f2) && $f2<>"")
  3. $query .= " and f2 = '$f2'";
  4. if(is_string($f3) && $f3<>"")
  5. $query .= " and f3 = '$f3'";
  6. .
  7. .
  8. .
  9. So on;
try this it will work
ssnaik84's Avatar
Member
 
Join Date: Aug 2009
Location: Bengaluru, India
Posts: 119
#2: Sep 25 '09

re: search data using more than one search fields


build a dynamic query..

Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT * FROM vehicles WHERE ";
  2. if($type != '') { $sql = $sql + " VehicleType='$type' AND " }
  3. if($make != '') { $sql = $sql + " VehicleMake='$make ' AND " }
  4. ...
  5. .. so on
well, i'm not good at PHP :)
Familiar Sight
 
Join Date: Oct 2008
Posts: 128
#3: Oct 2 '09

re: search data using more than one search fields


Quote:

Originally Posted by ssnaik84 View Post

build a dynamic query..

we have to make any one field necessary for this.howz it possible if users doesnt enter any field than all display
Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#4: Oct 2 '09

re: search data using more than one search fields


Still I'm confusing. Because of "AND" operator It didn't get any results.
Familiar Sight
 
Join Date: Oct 2008
Posts: 128
#5: Oct 3 '09

re: search data using more than one search fields


Quote:

Originally Posted by ghjk View Post

Still I'm confusing. Because of "AND" operator It didn't get any results.


Expand|Select|Wrap|Line Numbers
  1.    1. $sql = "SELECT * FROM vehicles WHERE ";
  2.    2. if($type != '') {  $sql = $sql + " VehicleType='$type' " }
  3.    3. if($make != '') {$sql = $sql + " AND  VehicleMake='$make '  " }
  4.    4. if($make1 != '') {$sql = $sql + " AND  VehicleMake1='$make1 '  " }
  5.    5. ...
  6.    6. .. so on
make any one of the field neccessry to enter and make that field variable in the second line.while other field variables in remaining lines
Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#6: Oct 5 '09

re: search data using more than one search fields


When I check this code It gives an error message.
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT * FROM vehicles WHERE ";
  2. if($type != '') {$sql = $sql+ "vehicle_type = 'Car'"}
  3. if($Make != '') {$sql = $sql + " AND  VehicleMake='Zusuki'" }
  4.  
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if($type != '') {$sql = $sql+ "vehicle_type = 'Car'"}
if($Make != '') {$sql = $' at line 1
Familiar Sight
 
Join Date: Oct 2008
Posts: 128
#7: Oct 6 '09

re: search data using more than one search fields


Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT * FROM table where f1 = '$f1'";/*u have to make f1 necessary to fill*/
  2. if(is_string($f2) && $f2<>"")
  3. $query .= " and f2 = '$f2'";
  4. if(is_string($f3) && $f3<>"")
  5. $query .= " and f3 = '$f3'";
  6. .
  7. .
  8. .
  9. So on;
try this it will work
Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#8: Oct 13 '09

re: search data using more than one search fields


Thanx kkshansid. It's working.
Reply