Jump to content

sql query in a if statement?!?


illuz1on

Recommended Posts

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>");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/48363-sql-query-in-a-if-statement/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.