illuz1on Posted April 23, 2007 Share Posted April 23, 2007 Hey can anyone spot the error in here? I want it to make it so that if q="" then it doesnt execute the SQL query <? if( $_GET["action"] == "clubs" ){ $q1 = "clubs"; } elseif( $_GET["action"] == "bars" ){ $q1 = "bars"; } elseif( $_GET["action"] == "lounges" ){ $q1 = "lounges"; } elseif( $_GET["action"] == "events" ){ $q1 = "events"; }else { $q1 = ""; echo "Click on one of the above links to see a listing of some of the hottest places in town!"; } ?> <? if ( !q ) { echo "Click on one of the above links to see a listing of some of the hottest places in town!"; } else { $getnews = mysql_query("select * from $q1"); while($r=mysql_fetch_array($getnews)){ extract($r); echo("<div class=\"blue\"><b>$name - $id</b></div> <div class=\"sdesc\">$music</div> <hr>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48363-sql-query-in-a-if-statement/ Share on other sites More sharing options...
trq Posted April 23, 2007 Share Posted April 23, 2007 if ( !q ) { Should be... if ($q == "") { I'm sorry though, but most of your code is redundant. Quote Link to comment https://forums.phpfreaks.com/topic/48363-sql-query-in-a-if-statement/#findComment-236441 Share on other sites More sharing options...
illuz1on Posted April 23, 2007 Author Share Posted April 23, 2007 what do you mean? Quote Link to comment https://forums.phpfreaks.com/topic/48363-sql-query-in-a-if-statement/#findComment-236443 Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 Redundant code is a computer programming term for code that is executed but has no effect on the output of a program (dead code is the term applied to code that is never executed). Quote Link to comment https://forums.phpfreaks.com/topic/48363-sql-query-in-a-if-statement/#findComment-236449 Share on other sites More sharing options...
trq Posted April 23, 2007 Share Posted April 23, 2007 Most of it is not needed. <?php if (isset($_GET["action"])) { if ($result = mysql_query("SELECT * FROM " . mysql_real_escape_string($_GET['action'])); if (mysql_num_rows($result)) { while($row = mysql_fetch_array($result)) { extract($row); echo "<div class=\"blue\"><b>$name - $id</b></div><div class=\"sdesc\">$music</div><hr>"; } } } } else { echo "Click on one of the above links to see a listing of some of the hottest places in town!"; } ?> My code will do exactly the same thing + it has some form of error handling. Quote Link to comment https://forums.phpfreaks.com/topic/48363-sql-query-in-a-if-statement/#findComment-236451 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.