想要使用 mysqli 和 php 在 html 表上显示多行结果,它只显示第一行,但我的代码没有看到错误



我只从数据库中获取第一行

<?php require('testconfig.php') ?>
<html>
    <head>
    </head>
    <body>
    <?php 
        $sql="SELECT * FROM item ";
        $sqlll=mysqli_query($conn,$sql) or die ("cant fetch data");
        $result=mysqli_fetch_assoc($sqlll);
    ?>
<table border='1'>
<tr>
<td>r.no</td>
<td>name</td>
<td>prices</td>
</tr>
<?php 
    while ($row=mysqli_fetch_assoc($sqlll)) 
    {
        echo "<tr>";
        echo "<td>".$result['RNO']."</td>";
        echo "<td>".$result['ITEMNAME']."</td>";
        echo "<td>".$result['PRICE']."</td>"; 
        echo "</tr>";
        echo "<br/>";
    }
?>
</table>
</body>
</html>

我应该得到的是循环显示 html 表上查询的所有结果。

这是我的表名=测试

-------------------------------------
r.no  |itemname |price |leftinstore |
-------------------------------------
1     |ball     |98.45 |4           |
-------------------------------------
2     |book     |90    |7           |
-------------------------------------
3     |food     |68.98 |4           |
-------------------------------------

将 while 循环变量的名称更改为 result,一切应该都可以正常工作。在你不循环槽$result之前,你循环$row并在循环中使用$result。

<?php require('testconfig.php') ?>
    <html>
        <head>
        </head>
    <body>
    <?php 
    $sql="SELECT * FROM item ";
    $sqlll=mysqli_query($conn,$sql) or die ("cant fetch data");
    ?>
    <table border='1'>
    <tr>
    <td>r.no</td>
    <td>name</td>
    <td>prices</td>
    </tr>
    <?php 
        while ($result=mysqli_fetch_assoc($sqlll)) 
        {
    echo "<tr>";
    echo "<td>".$result['RNO']."</td>";
    echo "<td>".$result['ITEMNAME']."</td>";
    echo "<td>".$result['PRICE']."</td>"; 
    echo "</tr>";
    echo "<br/>";
    }
    ?>
    </table>
    </body>
    </html>

最新更新