2013-10-03 29 views
1

我正在使用PDO,並試圖在表格上打印結果,但值不出現。我需要使用<td>標籤。將結果打印到帶有PDO的表格

這是我與弗朗西斯的幫助完成代碼:

<?php 
    require_once('config.php'); 


    $stmt = $connect->prepare("SELECT id, username, email FROM user"); 
    $stmt->execute(); 
    $result = $stmt->fetch(PDO::FETCH_ASSOC); 

    ?> 


       <table id="table"> 
        <thead> 
         <tr> 
          <th>ID</th> 
          <th>Username</th> 
          <th>email</th> 

         </tr> 
        </thead> 
        <tbody> 
         <?php 
         foreach ($result as $row): 

          ?> 
          <tr> 
           <td><?= $row['id'];?></td> 
           <td><?= $row['username'];?></td> 
           <td><?= $row['email'];?></td> 

          </tr> 
          <?php 
         endforeach; 
         ?> 
        </tbody> 
       </table> 
+1

你生成的html是什麼? –

+0

我會深入研究它,到目前爲止,如果你想獲取每個用戶,它就是'fetchAll()'而不是'fetch()'。你有[錯誤處理設置爲嚴格和日誌記錄](http://stackoverflow.com/q/1248952/938236)?甚至在包括config.php之前做到這一點,並看看錯誤說什麼。 –

回答

0

你需要調用fetchAll()外環線OR呼叫fetch()在每次迭代這樣的:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
    // do sth 

如果您想要foreach,只需撥打fetchAll()並做foreach循環結果。