Display multiple rows in html

157 Views Asked by At

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

There are 3 best solutions below

2
On BEST ANSWER

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) ) {
?>
0
On

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

2
On

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