Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.