从MySQL表获取数据到PHP时不显示第一个数据

时间:2018-10-05 14:09:22

标签: php html mysql

我正在尝试从sql数据库中获取数据到php表中。但是问题是我数据库中的第一个数据没有出现在表中。我犯错了吗?以下是我的代码。...

<?php
include "koneksi.php";
$sql    = mysqli_query ($link,
        "SELECT * FROM absen");
$data   = mysqli_fetch_array($sql);
?>

<html>
<head>
<title>Data Mahasiswa</title>
</head> 
<body>

<p><h2><b><center>DATA MAHASISWA</center></b></h2></p>


<table border="2" style="1000px;" align="center">
    <tr bgcolor="blue">
        <th>No</th>
        <th>Nama</th>
        <th>NIM</th>
        <th>Jenis Kelamin</th>
    </tr>
    <?php
    while($data)
        while($data = mysqli_fetch_array($sql)){
    ?>
     <tr>
        <td><?php echo $data['no']; ?></td>
        <td><?php echo $data['nama']; ?></</td>
        <td><?php echo $data['nim']; ?></</td>
        <td><?php echo $data['jenis_kelamin']; ?></</td>
    </tr>
    <?php } ?>

</table>
<center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px"> 
</a></h3></b></center>
</p>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您执行mysqli_fetch_array()两次。固定代码:

    <?php
    include "koneksi.php";
    $sql    = mysqli_query ($link,
            "SELECT * FROM absen");

    ?>

    <html>
    <head>
    <title>Data Mahasiswa</title>
    </head> 
    <body>

    <p><h2><b><center>DATA MAHASISWA</center></b></h2></p>


    <table border="2" style="1000px;" align="center">
        <tr bgcolor="blue">
            <th>No</th>
            <th>Nama</th>
            <th>NIM</th>
            <th>Jenis Kelamin</th>
        </tr>
        <?php
          while($data = mysqli_fetch_array($sql)){
        ?>
         <tr>
            <td><?php echo $data['no']; ?></td>
            <td><?php echo $data['nama']; ?></</td>
            <td><?php echo $data['nim']; ?></</td>
            <td><?php echo $data['jenis_kelamin']; ?></</td>
        </tr>
        <?php } ?>

    </table>
    <center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px"> 
    </a></h3></b></center>
    </p>
    </body>
    </html>