如果从代码中删除WHERE,则它在其他时间不起作用

时间:2017-02-05 18:07:52

标签: php

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'];
      }

当我删除WHERE然后它没关系,否则它会给出错误

请帮帮我

我尝试了各种方式,但它没有发生。如果我删除WHERE标记,那么它正好发生

forum_topic structure

2 个答案:

答案 0 :(得分:-1)

使用此

$sub_query = "SELECT * FROM `forum_sub` WHERE s_id =". $sub_id;

答案 1 :(得分:-1)

如果在if语句之外,那么置于if语句中的变量是未定义的。

尝试

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'];
  }
}