php - MySQL Query not returning expected results -
lets have following information in table. -1 means unlimited
price minutes sms data 7.50 200 -1 250 10 250 -1 1000 12 250 -1 -1
on form i've got number of fields , checkbox if want unlimited of have sql query has few variables depending on if checkboxes ticked so:
//check see if minutes set unlimited. if are, set sms -1 if ($minunlim=="on"){ $minquery = "minutes = -1"; $minutes = "unlimited"; } else { $minutes = $_get["minutes"]; $minquery = "minutes >= '$minutes' or minutes = -1"; } //check see if sms set unlimited. if are, set sms -1 if ($smsunlim=="on"){ $smsquery = "sms = -1"; $sms = "unlimited"; } else { $sms = $_get["sms"]; $smsquery = "sms >= '$sms' or sms = -1"; } //check see if sms set unlimited. if are, set sms -1 if ($dataunlim=="on"){ $dataquery = "data = -1"; $data = "unlimited"; } else { $data = $_get["data"]; $dataquery = "data >= '$data' or data = -1"; } //run query $getprices = $this->db->query("select * gg_crates price <= '$price' , $minquery , ($smsquery) , ($dataquery)");
so criteria used price = 36, minutes = 100, sms = 200, data = 300 , expectations in instance, items returned row's price 10 , 12 (because data below 300 on first row)
the query outputs is:
select * gg_crates price <= '36' , minutes >= '100' or minutes = -1 , (sms >= '200' or sms = -1) , (data >= '300' or data = -1)
in mind reads
select price lower 36, minutes greater or equal 100 or equal -1. , sms greater or equal 200 or equal -1. , data greater or equal 300 or equal -1
there no in statement should pull 7.50 row data lower 250.
however actual results i'm getting it's returning 3 rows. suggestions?
you have problem in logic,
this
$smsquery = "sms >= '$sms' or sms = -1"
and this:
data >= '$data' or data = -1
is problematic representation, change query to:
$getprices = $this->db->query("select * gg_crates price <= '$price' , ($minquery) , ($smsquery) , ($dataquery)");
it fix it
edit k20: $minquery needed in brackets
Comments
Post a Comment