-6
if(isset($_GET['open'])){
    $sub_id = $_GET['open'];
    $sub_query = "SELECT * FROM `forum_sub` WHERE s_id = $sub_id";
    $sub_run  = mysqli_query($connection, $sub_query); 
    $sub_row  = mysqli_fetch_array($sub_run);
    $sub_name = $sub_row['title']; 
    }

/*** 
 * Error starts here: 
 ***/
$topic_query = "SELECT * FROM `forum_topic` WHERE categories = $sub_name";
$topic_run = mysqli_query($connection, $topic_query);

/***  
 * Note also here:
 ***/
 while($topic_row = mysqli_fetch_array($topic_run)){ 
       $p_id = $topic_row['id'];
       $p_title = $topic_row['subject'];
       $p_msg =$topic_row['msg'];
       $p_date = $topic_row['date'];
       $p_username = $topic_row['u_name'];
       $p_view = $topic_row['view'];
      }

When I Remove WHERE then it is okay otherwise it gives error

Please help me

I tried every way but it just not happening. if I remove WHERE tag then it is happening properly

forum_topic structure

Martin
  • 22,212
  • 11
  • 70
  • 132
T.mandal
  • 21
  • 7

2 Answers2

-1

use this

$sub_query = "SELECT * FROM `forum_sub` WHERE s_id =". $sub_id;
Arslan Bilal
  • 625
  • 2
  • 9
  • 17
-1

Your variables placed inside of the if statement are undefined when outside of the if statement.

Try

if(isset($_GET['open'])){
$sub_id = $_GET['open'];
$sub_query = "SELECT * FROM `forum_sub` WHERE s_id = $sub_id";
$sub_run  = mysqli_query($connection, $sub_query); 
$sub_row  = mysqli_fetch_array($sub_run);
$sub_name = $sub_row['title']; 

   $topic_query = "SELECT * FROM `forum_topic` WHERE categories = $sub_name";
   $topic_run = mysqli_query($connection, $topic_query);

   while($topic_row = mysqli_fetch_array($topic_run)){
   $p_id = $topic_row['id'];
   $p_title = $topic_row['subject'];
   $p_msg =$topic_row['msg'];
   $p_date = $topic_row['date'];
   $p_username = $topic_row['u_name'];
   $p_view = $topic_row['view'];
  }
}
Loveen Dyall
  • 824
  • 2
  • 8
  • 20