mysql - Optimize SQL Query -


i've 5 dropdowns user can select values fetch data table. values of dropdowns distinct values coming different columns of table. each dropdowns have option "all". i'm storing user selections in 5 variables i.e variablename , variablename1 , on... default dropdown value all...

currently i'm using 32 different queries if , else-if conditions fetch data .. i.e

if($variablename=="all" && $variablename2=="all" && $variablename5=="all" && $variablename6=="all" && $variablename7=="all") { $query="select * table ....}     else if($variablename!="all" && $variablename2!="all" && $variablename5!="all" && $variablename6!="all" && $variablename7!="all")         { $query= "select * table   abc = '$variablename' , bcd = '$variablename1' , cde= '$variablename2'     , def= '$variablename4' , efg= '$variablename4' ; ...} 

note !=all means user have select value dropdown except option.

in way i've make 32 different queries. how can make more optimize query rid of making 32 different queries ? hope question ..

$query = "select * table"; $conditions = array(); if ($variablename !== "all") {   $conditions[] = "abc = '" . mysqli_real_escape_string($variablename) . "'"; } if ($variablename2 !== "all") {   $conditions[] = "bcd = '" . mysqli_real_escape_string($variablename2) . "'"; } // ... if ($conditions) {   $query .= "where " . implode(" , ", $conditions); } 

beware bobby tables! also, nicer if had array, , not 5 different variables.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -