2012-10-21 92 views
0

因此,我一直在研究一個小型系統,其中涉及用於用戶兌換它們以獲得某種獎勵的代碼。PHP只顯示來自數據庫的單個結果

到目前爲止,我有在PHP這個腳本和HTML: http://pastebin.com/UUsEKpev

這只是顯示一個結果,你可以看到here,我希望它顯示錶中的多個結果走下去顯示所有結果。

<?php 

$yn; 
mysql_connect("localhost", "root", "password") or die(mysql_error()); 
mysql_select_db("mcv") or die(mysql_error()); 

$result = mysql_query("SELECT * FROM Codes") 
or die(mysql_error()); 

$row = mysql_fetch_array($result); 
// Print out the contents of the entry 

if ($row['Used'] == 1) { 
    $yn = "Yes"; 
} 
else{ 
    $yn = "No"; 
} 

?> 

<html> 
<head> 
    <title>Minecraft Codes</title> 
    <style type="text/css"> 

    tbody { 
     display: table-row-group; 
     vertical-align: middle; 
     border-color: inherit; 
    } 

    tr { 
     display: table-row; 
     vertical-align: inherit; 
     border-color: inherit; 
    } 

    th { 
     border: 1px solid #C3C3C3; 
     padding: 3px; 
     vertical-align: top; 
     width: 20%; 
     background-color: #E5EECC; 
    } 

    table.reference { 
     background-color: white; 
     border: 1px solid #C3C3C3; 
     border-collapse: collapse; 
     width: 50%; 
    } 

    table.reference td { 
     border: 1px solid #C3C3C3; 
     padding: 3px; 
     vertical-align: top; 
     width: 20%; 
    } 

    table, th, td, input, textarea { 
     font-size: 100%; 
    } 

    body, p, h1, h2, h3, h4, table, td, th, ul, ol, textarea, input { 
     font-family: verdana,helvetica,arial,sans-serif; 
    } 

    </style> 
</head> 

<body> 
    <center> 
     <br /> 
     <h1><u>Minecraft Server VIP Codes</u></h1> 
     <br /> 

     <table class="reference"> 
      <tr> 
       <th>Code</th> 
       <th>Used?</th> 
      </tr> 
      <?php echo "<tr><td>".$row['Code']."</td><td>".$yn."</td></tr>"; ?> 
     </table> 
    </center> 
</body> 
</html> 
+0

它是在這裏歡迎添加代碼,甚至是問題的結果,讓它變得完整和一致。 – ppeterka

+0

mysql_fetch_array返回查詢結果的單個ROW,而不是整個結果集。 –

回答

0

好的,你只需要一次調用mysql_fetch_row(),因此你只需要檢索一行。你必須將其包裝到一個循環中,該循環的執行次數與行數相同。

有上百萬的,你可以借鑑互聯網上的例子...

1

你的問題是,你只能讀取一行:

$row = mysql_fetch_array($result); 

該行獲取的當前行結果集。你想這樣做,在一個循環:

$rows = array(); 
while ($row = mysql_fetch_array($result)) { 
    $rows[] = $row; 
} 

如果可以請避免使用mysql_函數,它們被棄用,看到巨大的警告hereread this

+0

...或立即顯示而不存儲 –

1
<table class="reference"> 
    <tr> 
     <th>Code</th> 
     <th>Used?</th> 
    </tr> 

    <?php while ($row = mysql_fetch_array($result)): ?> 
    <?php 
     if ($row['Used'] == 1) { 
      $yn = "Yes"; 
     } 
     else{ 
      $yn = "No"; 
     } 
    ?> 
    <tr> 
     <td><?php echo $row['Code']; ?></td> 
     <td><?php echo $yn; ?></td> 
    </tr> 
    <?php endwhile; ?> 
</table> 
+0

不知道while(...):... endwhile;'是一個有效的語法。我習慣花括號。 –

+0

是的!那也是存在的,我已經重新編輯過,以便其他人可以像你那樣理解可讀性因素。不要擔心,我們一步一步地學習,而且我們在出生時並沒有開始跑步。 – Rafee

+0

無法讓這個工作,結果要麼出錯或代碼行不顯示。 –