简体   繁体   English

在动态页面上使用PHP while循环不起作用

[英]Using PHP while loop on a dynamic page won't work

I am trying to show it all with while loop on a dynamic page, but it wont show anything at all.. 我正在尝试通过动态页面上的while循环来显示所有内容,但是它根本不会显示任何内容。

if (isset($_GET['id'])) {
      $genre = $_GET['id'];
      $sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
      $result = $db->query($sql);
      $row = $result->fetch_assoc();
}else{
     $sql = "SELECT ID, genre FROM movstream_genre";
     $result = $db->query($sql);
}

<html>
<body>
<ul>
    <?php while ( $row = $result->fetch_assoc() ): ?>
    <li><a href="genre?id=<?=$row['ID'];?>"><?=$row['genre'];?></a></li>
    <?php endwhile; ?>
</ul>
</body>
</html>

Anyone know why it wont show anything on the dynamic page, but if the page is not dynamic it works fine :) 任何人都知道为什么它不会在动态页面上显示任何内容,但是如果该页面不是动态页面,则可以正常工作:)

Thanks. 谢谢。

If dynamic page means that your sending an id in Url an you are not getting result. 如果动态页面意味着您在Url中发送id ,则不会得到结果。 I think that the problem is that you are using $row = $result->fetch_assoc(); 我认为问题在于您正在使用$row = $result->fetch_assoc(); in if condition and also in while. if条件以及在一段时间。 Please use fetching in one place. 请在一个地方使用抓取功能。 Better be in while loop. 最好在while循环中。 Hope it helps. 希望能帮助到你。

please echo the result ... 请回显结果...

<li><a href="genre?id=<?php echo $row['ID'];?>"><?php echo $row['genre'];?></a></li>

And in href, it is only genre OR genre.php 在href中,这只是genre或genre.php

In the first query.. 在第一个查询中..

$sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
$result = $db->query($sql);
$row = $result->fetch_assoc();// this line not required

comment this line $row = $result->fetch_assoc(); 评论这一行$ row = $ result-> fetch_assoc();

I figured it out 我想到了

if (isset($_GET['id'])) {
$genre = $_GET['id'];

$sql = "SELECT ID, genre FROM movstream_genre";
$result = $db->query($sql);
}

This it how it needs to look when its a dynamic page :) well i think it is, it works now. 这是它需要一个动态页面时的样子:)我认为是的,它现在可以工作了。

好像您没有将PHP代码的第一部分包含在<?php ?>标记中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM