在 html 中显示多行

Display multiple rows in html

我在从数据库中提取信息并将其显示在页面上时遇到问题。在 table 中,我在每一列中都有多个条目。所以我需要提取该信息并从列 id 开始按降序显示它。但是有了这个它在页面上显示这样的行:

### ### ###
#1# #1# #1#
### ### ###

### ### ###
#2# #2# #2#
### ### ###

### ### ###
#3# #3# #3#
### ### ###

我不知道我到底做错了什么。自从我用 php 和 mysql 做任何事情以来已经有一段时间了,所以我可能只是写错了一些东西,但我不知道。

<?
require 'dbinfo.php';

   $link = mysqli_connect($servername, $username, $password);
    if (!$link) {
    die('Could not connect: ' . mysqli_error($link));
    }
    mysqli_select_db($link, $database) or die(mysqli_error($link));

    $query = "SELECT `id`, `build`, `buildby`, `description`, `download` FROM `buildlist` ORDER BY id DESC";
    $result = mysqli_query($link,$query) or die(mysqli_error($link));

    $row = mysqli_fetch_row($result);
    $num = mysqli_num_rows($result);
    ?>

<?
      $num = mysqli_num_rows($result);
      if($num) {
        while( $row = mysqli_fetch_array($result) ) {
    ?>

    <article class="one_third">
      <h2>Build <? echo $row['build'] ?></h2>
      <img src="images/80x80.gif" alt="">
      <p>Build by <? echo $row['buildby'] ?><br />
      <br /><br /><br />
      <? echo $row['description'] ?><br />
      <br / >
      <a href="<? echo $row['download'] ?>" class="download">Download this build</a></p><br />
    </article>

    <article class="one_third midbox">
      <h2>Build <? echo $row['build'] ?></h2>
      <img src="images/80x80.gif" alt="">
      <p>Build by <? echo $row['buildby'] ?><br />
      <br /><br /><br />
      <? echo $row['description'] ?><br />
      <br / >
      <a href="<? echo $row['download'] ?>" class="download">Download this build</a></p><br /><br /><br />
    </article>

    <article class="one_third lastbox">
      <h2>Build <? echo $row['build'] ?></h2>
      <img src="images/80x80.gif" alt="">
      <p>Build by <? echo $row['buildby'] ?><br />
      <br /><br /><br />
      <? echo $row['description'] ?><br />
      <br / >
      <a href="<? echo $row['download'] ?>" class="download">Download this build</a></p><br />
    </article>

    <? }} ?>

您的查询有一些错误 - 不需要撇号。以下查询应该可以解决问题:

SELECT id, build, buildby, description, download FROM buildlist ORDER BY id DESC

有用吗? :)

您在第 10、11 行有附加代码。在第 15 行而不是 if($num)if($num>0)。在第 16 行中,将 $row = mysqli_fetch_array($result) 写成 $row = mysqli_fetch_array($result,MYSQLI_ASSOC)。使用 <?php 而不是 <?.

您的代码中有一些错误。首先,您不需要在循环之前获取一行。其次 - 你应该使用 mysqli_fetch_assoc 而不是 mysqli_fetch_array 或者使用 $row[index] 而不是 $row[string].

正确代码如下:

<?
    require 'dbinfo.php';

    $link = mysqli_connect($servername, $username, $password);

    if (!$link) {
        die('Could not connect: ' . mysqli_error($link));
    }
    mysqli_select_db($link, $database) or die(mysqli_error($link));

    $query = "SELECT `id`, `build`, `buildby`, `description`, `download` FROM `buildlist` ORDER BY id DESC";
    $result = mysqli_query($link,$query) or die(mysqli_error($link));

    $num = mysqli_num_rows($result);
    if($num) {
        while( $row = mysqli_fetch_assoc($result) ) {
?>