2016-02-11 120 views
-5

我一直在尋找一段時間,我一直在試圖將我的數據從我的數據庫放到我的html表格中。我如何實現這一目標?這是我的代碼。PHP/PDO將數據庫中的數據轉換爲表格

CONFIG.PHP

<?php 
try { 
    $db = new PDO("mysql:host=127.0.0.1;dbname=boekingdb", "root", ""); 
} catch (PDOException $e) { 
    print($e->getMessage()); 
} 

PHP代碼:

<?php 
$getUser = $db->prepare("SELECT * FROM users"); 
$getUser->execute(); 
$users = $getUsers->fetchAll(); 
foreach ($users as $user) { 
    echo $user['username']; 
    echo $user['password']; 
} ?> 
+1

'' –

+1

你爲什麼要逃避括號 - '{$行\ [ '用戶名' \]}'? – Sean

+1

'$ row'是什麼? – chris85

回答

0

好了,所以我已經想通了

<?php 
$getUser = $db->prepare("SELECT * FROM users"); 
$getUser->execute(); 
$users = $getUsers->fetchAll(); 

<table> 
    <tr> 
     <th>Username</th> 
     <th>Password</th> 
    </tr> 
<?php foreach ($users as $user) { ?> 
<tr> 
    <td><?php echo $users['username'] ?></td> 
    <td><?php echo $users['password'] ?></td> 
</tr> 
<?php } ?> 
</table> 

現在Echo的表內的每個用戶名和密碼。即使我在數據庫中添加新的數據庫,我只需要刷新並添加它們。

對不起,以混亂的方式寫我的問題。

+0

這是行不通的。在你的foreach循環中'$ users'!='$ user',你仍然沒有連接。你不能與我們分享整個圖片。 –

0

所以我編輯了我的項目需要的東西的名稱和東西,但它仍然非常相似。我也做了select語句的一個函數,並把它放在一個外部文件中。這裏有一些截圖。

This is the functions.php file where I require the config.php file (Config.php is where my connection is written)

This is the index.php where I include the functions.php file and also make the for each loop "I've moved the for each loop down into the table for it to work"

相關問題