在sql中不起作用(where ...和...)

时间:2017-02-23 17:22:16

标签: php mysqli

全部代码

    <?php
$subject;
$query="SELECT * ";
$query.="FROM SUBJECTS ";
$query.="WHERE VISIBLE = 1 ";
$query.="ORDER BY POSITION ASC"; 
$subject_set=mysqli_query($connection,$query);
Confrim_query($subject_set);
?>
<?php
while($subject=mysqli_fetch_assoc($subject_set)){?>
    <li><?php echo $subject["MENU_NAME"] . "(".$subject["ID"].")"; ?>
    <?php  }?>
    <?php
$query="SELECT * ";
$query .="FROM PAGES ";
$query .="WHERE VISIBLE = 1 ";
$query .="AND SUBJECT_ID = {$subject['ID']"} ;
$query .="ORDER BY POSITION ASC";
$page_set=mysqli_query($connection,$query);
Confrim_query($page_set);

?>

但是这部分代码没有在页面中运行和没有显示

   <ul class="page">
    <?php
    while($page=mysqli_fetch_assoc($page_set)){ ?>
    <li><?php echo $page["MENU_NAME"] ?></li>
    <?php }?>
    <?php mysqli_free_result($page_set); ?>
    </ul>
    </li>
    <?php  mysqli_free_result($subject_set);  ?>

    </ul>

添加时:

  

$ query。=“AND SUBJECT_ID = {$ subject ['ID']”};

此部分无法重新加载数据库并失败。

2 个答案:

答案 0 :(得分:0)

$subject['ID']前面的字符串中有一个大括号,在字符串外面有一个大括号 我不认为您需要大括号来使查询工作,尝试删除它们并查看它是否有效。

SELECT * FROM PAGES WHERE VISIBLE = 1 AND SUBJECT_ID = $subject['ID'] ORDER BY POSITION ASC;

答案 1 :(得分:0)

我找到了答案......什么是问题 之后

while ($subject = mysqli_fetch_assoc($subject_set))
{?>
<li><?PHP echo $subject['MENU_NAME'] . "(" . $subject['ID'] . ")";

必须添加查询

$query = "SELECT * ";
$query .= "FROM PAGES ";
$query .= "WHERE SUBJECT_ID={$subject['ID']} ";
$query .= "ORDER BY SUBJECT_ID ASC ";
$page_set = mysqli_query($connection, $query);
Confrim_query($page_set);