PHP代码的问题...在阵列中的数据,但在HTML中显示记录时,失去了一些东西?

问题描述 投票:-2回答:1

这是一个相当简单的DB请求,阵列“数据”显示的正确使用pre_r()函数,但我正在与这个HTML表侧的问题的时候,我不能让它显示任何记录。我失去了一些东西很明显这里是躲避我,任何帮助吗?

<?php
$mysqli= new mysqli('localhost', 'username', 'password', 'DB') or             die(mysqli_error($mysqli));
$data = $mysqli -> query("SELECT * FROM line_job WHERE JOB_NO=36934 AND LINE_NO=2") or die($mysqli->error);

pre_r($data ->fetch_assoc());   //recordset array

function pre_r( $array ) {
    echo '<pre>';
print_r($array);
echo '</pre>';
}


?>
<!doctype html>
<html lang="en">
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" src="/css/bootstrap.min.css" >
<link href="/css/bootstrap.css" rel="stylesheet" type="text/css">
  <title>[![enter image description here][1]][1]Detail</title>
</head>

<body>
<div class="row justify-content-center">
<div class="container">
    <table class="table">
        <thead>
            <tr>
                <th>Status</th>
                <th>Job Type</th>
                <th>Resp. Party</th>
            </tr>

        </thead>
        <?php 
            while ($data = $data->fetch_assoc()): ?>
            <tr>
                <td><?php echo $data['STATUS'];?></td>
                <td><?php echo $data['JOB_TYPE'];?></td>
                <td><?php echo $data['RESP_PRTY'];?></td>
            </tr>
            <?php endwhile;?>

    </table>
  </div>
</div>

</body>
</html>
php html
1个回答
0
投票

$mysqli ->query()返回mysqli_result对象。这个对象指向第一个记录的指针。每次你打电话fetch_assoc()就可以了,它由一个返回单个记录,并递增指针指向下一行如果没有更多的行返回NULL。

显然,你的MySQL查询返回一行。通过调用$data->fetch_assoc()第二次的时候,有没有更多的行留在$数据获取。

© www.soinside.com 2019 - 2024. All rights reserved.