Clinton Posted January 6, 2009 Share Posted January 6, 2009 I didn't think it was that complicated but everytime I add another variable it seems to not work. This time it's the state variable. I added it and my query is pretending like it's not there and displaying the rest of the result. ANy ideas? $sql = "SELECT * FROM $tbl_name WHERE dtype = '$dtype' OR dtypewc = '$dtype' AND dmajorp = '$smajor' OR dmajorc1 = '$smajor' OR dmajorc2 = '$smajor' OR dmajorc3 = '$smajor' AND state = '$state' ORDER BY jpted LIMIT $start, $limit"; Link to comment https://forums.phpfreaks.com/topic/139694-solved-complicated-select-query/ Share on other sites More sharing options...
gevans Posted January 6, 2009 Share Posted January 6, 2009 You're mixing OR and AND you need to bracket off some parts of it as it makes sense to you () Link to comment https://forums.phpfreaks.com/topic/139694-solved-complicated-select-query/#findComment-730901 Share on other sites More sharing options...
Clinton Posted January 6, 2009 Author Share Posted January 6, 2009 Thanks Gevans, that worked. Now one more question. If the state variable hasn't been selected I want them to be able to view all states. Do I have to do an if isset statement or can I use a * or a wildcard variable of somesort? I've tried both * and % to no avail. Link to comment https://forums.phpfreaks.com/topic/139694-solved-complicated-select-query/#findComment-730911 Share on other sites More sharing options...
flyhoney Posted January 6, 2009 Share Posted January 6, 2009 Maybe this: WHERE ... (state = '$state OR state LIKE '%%') ... Link to comment https://forums.phpfreaks.com/topic/139694-solved-complicated-select-query/#findComment-730917 Share on other sites More sharing options...
cytech Posted January 6, 2009 Share Posted January 6, 2009 $state_where = ""; if(!empty($state)){ $state_where = " AND state = {$state}"; } $sql = "SELECT * FROM $tbl_name WHERE dtype = '$dtype' OR dtypewc = '$dtype' AND dmajorp = '$smajor' OR dmajorc1 = '$smajor' OR dmajorc2 = '$smajor' OR dmajorc3 = '$smajor' {$state_where} ORDER BY jpted LIMIT $start, $limit"; Thats how I would go about it. Link to comment https://forums.phpfreaks.com/topic/139694-solved-complicated-select-query/#findComment-730918 Share on other sites More sharing options...
Clinton Posted January 6, 2009 Author Share Posted January 6, 2009 Thanks Cy, that worked perfectly. Link to comment https://forums.phpfreaks.com/topic/139694-solved-complicated-select-query/#findComment-730929 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.