PHP仅打印一行

时间:2019-04-21 14:32:30

标签: php html mysql

如您所见,我正在从数据库中选择用户,但它只能打印一个! 我有10个需要打印的用户。

<?php
      session_start();
      $currentgroup = $_SESSION['groupname'];
      $sql1 = "SELECT * FROM hjuma_users WHERE group1 ='$currentgroup' OR group2 ='$currentgroup' OR group3 ='$currentgroup' OR group4 ='$currentgroup' OR group5 ='$currentgroup';";
      if($result1 = mysqli_query($conn, $sql1)){
        if(mysqli_num_rows($result1) > 0){
            while($row1 = mysqli_fetch_array($result1)){ ?>


<div id="id01" class="modal">
  <form class="modal-content animate" action="/action_page.php">
    <div class="imgcontainer">
      <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
    </div>
    <div class="container">
      <?php echo $row1['username']; ?>

    </div>


  </form>
</div>
<?php   }
      }
    }
      ?>

3 个答案:

答案 0 :(得分:0)

您应该使用mysqli_fetch_assoc如下代码:

session_start();
$currentgroup = $_SESSION['groupname'];
$sql1 = "SELECT * FROM hjuma_users WHERE group1 ='$currentgroup' OR group2 ='$currentgroup' OR group3 ='$currentgroup' OR group4 ='$currentgroup' OR group5 ='$currentgroup';";
if ($result1 = mysqli_query($conn, $sql1)) {
    if (mysqli_num_rows($result1) > 0) {
        $modalId = 1;
        while ($row1 = mysqli_fetch_assoc($result1)) { ?>
            <div id="id<?php echo $modalId; ?>" class = "modal">
            <form class = "modal-content animate" action = "/action_page.php">
            <div class = "imgcontainer">
            <span onclick = "document.getElementById('id<?php echo $modalId; ?>').style.display='none'" class = "close" title = "Close Modal">&times;
            </span>
            </div>
            <div class = "container">
            <?php echo $row1['username'];
?>

            </div>


            </form>
            </div>
        <?php
        $modalId++;
        }
    }
}

答案 1 :(得分:0)

您似乎没有增加$ result1

答案 2 :(得分:0)

请检查$ currentgroup的值。

如果可以提供数据库表hjuma_users的屏幕快照以及$ currentgroup的值。

我认为由于这种情况,您获得了一个价值

WHERE group1 ='$currentgroup' OR group2 ='$currentgroup' OR group3 ='$currentgroup' OR group4 ='$currentgroup' OR group5 ='$currentgroup