brown2005 Posted July 31, 2009 Share Posted July 31, 2009 i have $_GET['county']; and $sql = mysql_query("SELECT * FROM comps ORDER BY date DESC"); now what i want to do is say if $_GET['county']; is null $sql = mysql_query("SELECT * FROM comps ORDER BY date DESC"); and if not null $sql = mysql_query("SELECT * FROM comps WHERE county='$_GET['county']' ORDER BY date DESC"); what is the easiest way to do this please. Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted July 31, 2009 Share Posted July 31, 2009 i think this would work <?php if(isset($_GET['country'])) { $queryCountry = "WHERE county='$_GET['county']'"; } else { $queryCountry = ""; } $sql = mysql_query("SELECT * FROM comps {$queryCountry} ORDER BY date DESC"); Quote Link to comment Share on other sites More sharing options...
Mardoxx Posted July 31, 2009 Share Posted July 31, 2009 <?php if (isset($_GET['county']) && $_GET['county'] != '') { $sql = mysql_query("SELECT * FROM comps WHERE county='$_GET['county']' ORDER BY date DESC"); } else { $sql = mysql_query("SELECT * FROM comps ORDER BY date DESC"); } <?php $country = $_GET['county']; $sql_stuff = "SELECT * FROM comps "; //note the space if (isset($country) && $country != '') { $sql_stuff .= "WHERE county='$_GET['county']' "; //note the space } $sql_stuff .= "ORDER BY date DESC"; $sql = mysql_query($sql_stuff); Not sure if this will work (but it should do): <?php $sql = (isset($_GET['county']) && $_GET['county'] != '') ? mysql_query("SELECT * FROM comps WHERE county='$_GET['county']' ORDER BY date DESC") : mysql_query("SELECT * FROM comps ORDER BY date DESC"); Quote Link to comment Share on other sites More sharing options...
brown2005 Posted July 31, 2009 Author Share Posted July 31, 2009 lonewolf217 - yours does not seem to work Mardoxx - all yours works.. which moves me onto my next question.. which one should I use? which is better practice? which is faster? Thanks Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted July 31, 2009 Share Posted July 31, 2009 there should be marginal difference between all of them since the only difference is the generation of the query string. Quote Link to comment Share on other sites More sharing options...
Mardoxx Posted July 31, 2009 Share Posted July 31, 2009 The first and last one I posted are probably quickest (very marginally as lonewolf217 said)... since they do two (mandatory) comparisons and one string assign... where as the middle one does one string assign, two (mandatory) comparisons and then a string append. IDK? make them loop about 10000x then compare the time it takes to execute Quote Link to comment 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.