PHP / PDO将数据从数据库回送到表中

时间:2016-02-11 18:44:14

标签: php mysql pdo

我一直在寻找一段时间,我一直在尝试将我的数据库中的数据放入我的html表中。我该如何实现这一目标?这是我的代码。

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'];
} ?>

2 个答案:

答案 0 :(得分: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>

现在回显表中的每个用户名和密码。即使我在数据库中添加新的,我只是点击刷新并添加它们。

很抱歉以混乱的方式写下我的问题。

答案 1 :(得分: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"