donglang9880
donglang9880
2015-06-14 08:59
浏览 187
已采纳

在html中显示多行

I am having an issue with pulling info from the database and displaying it on the page. In the table I have multiple entries in each column. So I need to pull that info and display it in a descending order from the column id. But with this it displays the rows like this on the page:

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

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

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

I can't figure out what exactly I am doing wrong here. It's been a little bit since I did anything with php and mysql so I may just be writing something incorrectly but I don't know.

<?
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>

    <? }} ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtcyv3985
    dtcyv3985 2015-06-14 09:41
    已采纳

    There is some mistakes in your code. First you don't need to fetch a row before the loop. Second - you should use mysqli_fetch_assoc instead of mysqli_fetch_array or use $row[index] instead of $row[string].

    Here is correct code:

    <?
        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) ) {
    ?>
    
    点赞 评论
  • dougang5088
    dougang5088 2015-06-14 09:17

    You have some mistakes in your query - the apostrophes are not required. The following query should solve the problem:

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

    Does it work? :)

    点赞 评论
  • douzhai7873
    douzhai7873 2015-06-14 09:34

    You have the additional code in line 10, 11. In line 15 instead of if($num) write if($num>0). In line 16 instead of $row = mysqli_fetch_array($result) write $row = mysqli_fetch_array($result,MYSQLI_ASSOC). Use <?php instead of <?.

    点赞 评论

相关推荐