php - How to optimize queries in mysql -


my mysql query search :

$result = mysql_query("select * room_tb location ='{$location}' , price between '$minprice' , '$maxprice' ")or die('could not connect: ' . mysql_error()); ; 

this makes compulsion enter both location , min , max price in form. want query can make user enter either location or max , min price, allow user search both fields. should do?

thsi ought :

$query = "select * room_tb ";  if($location){ $query .= " location ='{$location}' "; }  if(($minprice)&&($maxprice)&&(!$location)){ $query .= " price between '$minprice' , '$maxprice'"; }  if(($minprice)&&($maxprice)&&($location)){ $query .= " , price between '$minprice' , '$maxprice'"; }  $result = mysql_query($query)or die('could not connect: ' . mysql_error()); 

cheers


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -