2016-02-11 122 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"

相关问题