2015-06-14 65 views
-1

我在從數據庫中提取信息並將其顯示在頁面上時遇到問題。在表格中,我在每列中有多個條目。所以我需要將這些信息以列ID的降序顯示出來。但有了它,它在頁面上顯示這樣的行:在html中顯示多行

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

    <? }} ?> 
+0

通過跳過第一行,所以如果只有一個循環之前獲取的行,你就什麼也看不見。 – jeroen

回答

0

你的代碼有一些錯誤。首先你不需要在循環之前獲取一行。其次 - 您應該使用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)) { 
?> 
+0

清理了一下,謝謝。我仍然遇到了正確顯示提取行的問題。看到裏面有#的第一個代碼塊。 – gravvy

+0

你的意思是升序而不是降序嗎? – kulaeff

-2

您在查詢時出現了一些錯誤 - 撇號不是必需的。以下查詢應該可以解決問題:

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

它工作嗎? :)

+1

反引號並不是必需的,但它們可以防止出現名爲'mysql reserved words'的錯誤。 – SuperDJ

+0

在這種情況下,它們不是必需的,因爲列名或表名都不是保留字。當然不會傷害。 – jeroen

0

您在第10,11行中有附加代碼。在行15中代替if($num)寫入if($num>0)。在第16行而不是$row = mysqli_fetch_array($result)$row = mysqli_fetch_array($result,MYSQLI_ASSOC)。使用<?php而不是<?