0

海。我知道有类似的答案,但我无法得到它。这是我的鳕鱼:

<?php include 'header.php'; ?>
<?php include 'connect.php';

$q = mysqli_query($link, $aaa = 'SELECT * FROM agentii LEFT JOIN orase ON agentii.oras=orase.oras_id WHERE orase.oras_id = \'' .$_GET['id']. '\'') or die(mysqli_error($link));
$row = mysqli_fetch_assoc($q);
?>

<h2>Title <em><?php echo $row['oras'];?></em></h2>

<div class="div_view">
    <table cellspacing="0" cellpadding="5" border="0" width="100%">
        <tr>
            <th>Agentie</th>
            <th>Adresa</th>
        </tr>

        <?php while ($row = mysqli_fetch_assoc($q)) { ?>

        <tr>
            <td><?php echo $row['agentie'];?></td>
            <td><?php echo $row['adresa'];?></td>
        </tr>

        <?php }?>

    </table>
</div>

<?php include 'footer.php'; ?>

这个输出(我不能放图像,因为是一个新帐户 - 所以我放了一个指向 prt src 的链接): http: //postimage.org/image/w9zsexz91/

在我的数据库中,我有 3 行,代码只输出 2 行。我知道这是因为 2 mysqli_fetch_assoc,但我想显示所有行,并且我也想<?php echo $row['oras'];?>在 while 之外显示标题()。

有人可以帮我吗?提前谢谢!

4

2 回答 2

1

首先是强制性警告:记住小鲍比桌! (或者:您的代码容易受到 sql 注入攻击)

然后:您在 while 循环中只得到两行,因为您首先在循环外请求了一行,从而增加了结果中的内部游标。

如果要一次性获取标题和行,则需要缓存结果,例如:

$resultArray = array();
while ($row = mysqli_fetch_assoc($q)) {
  $resultArray[] = $row;
}
//....
?>
<h2><?php echo $resultArray[0]['oras']; ?></h2>

<?php // ... ?>

<?php foreach($resultArray as $row) {
  <tr>
    <td><?php echo $row['agentie'];?></td>
    <td><?php echo $row['adresa'];?></td>
  </tr>
} ?>

这只是一个存根,当然,如果你有一行,你应该实施检查等等......

于 2012-08-14T07:53:54.867 回答
0

尝试:

<?php do { ?>

<tr>
    <td><?php echo $row['agentie'];?></td>
    <td><?php echo $row['adresa'];?></td>
</tr>

<?php } while ($row = mysqli_fetch_assoc($q)); ?>

此循环的第一次迭代将打印第一个获取的行。我只更改了while子句的位置,强制循环的代码在获取新行之前运行。

于 2012-08-14T07:42:26.713 回答