Clinton Posted January 5, 2009 Share Posted January 5, 2009 I did the code below because I'm going to replace dtype = * with dtype = $variable and I wanted to get it to work... which it's not. Basically, I want to be able to narrow down the search by dtype AND the dmajors but if there is no specific dtype selected I want to show all of the dtypes. Make sense? <?php $query = "SELECT COUNT(*) as num FROM $tbl_name WHERE dtype = * AND dmajorp = '$smajor' OR dmajorc1 = '$smajor' OR dmajorc2 = '$smajor' OR dmajorc3 = '$smajor' ORDER BY jpted"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/139606-solved-where/ Share on other sites More sharing options...
Brian W Posted January 5, 2009 Share Posted January 5, 2009 *edited because i misunderstood question* You need to have to separate queries depending on whether or not $smajor isset Quote Link to comment https://forums.phpfreaks.com/topic/139606-solved-where/#findComment-730370 Share on other sites More sharing options...
Adam Posted January 5, 2009 Share Posted January 5, 2009 Just seen Brian's post, try that, if it doesn't work give below a bash! ----- Perhaps you could do this PHP side? Something like... $dtype = (some condition) ? "dtype = 'dtype' AND" : ""; $query = "SELECT COUNT(*) as num FROM $tbl_name WHERE {$dtype} dmajorp = '$smajor' OR dmajorc1 = '$smajor' OR dmajorc2 = '$smajor' OR dmajorc3 = '$smajor' ORDER BY jpted"; A Quote Link to comment https://forums.phpfreaks.com/topic/139606-solved-where/#findComment-730372 Share on other sites More sharing options...
Brian W Posted January 5, 2009 Share Posted January 5, 2009 <?php if(!isset($dtype) || $dtype == ''){ $dtype = "-1"; } $query = "SELECT COUNT(*) as num FROM $tbl_name WHERE dtype = $dtype AND dmajorp = '$smajor' OR dmajorc1 = '$smajor' OR dmajorc2 = '$smajor' OR dmajorc3 = '$smajor' ORDER BY jpted"; ?> is that what you are looking for? Quote Link to comment https://forums.phpfreaks.com/topic/139606-solved-where/#findComment-730380 Share on other sites More sharing options...
Clinton Posted January 5, 2009 Author Share Posted January 5, 2009 Ok, thanks guys, yea, both of those work. :-) Quote Link to comment https://forums.phpfreaks.com/topic/139606-solved-where/#findComment-730384 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.