【问题标题】:PHP / PDO echo data from database into a tablePHP / PDO 将数据库中的数据回显到表中
【发布时间】:2016-05-22 17:05:04
【问题描述】:

我已经搜索了一段时间,并且一直在尝试将我的数据从我的数据库中放入我的 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'];
} ?>

【问题讨论】:

  • &lt;?php echo $row['username'] ?&gt;
  • 你为什么要转义括号 - {$row\['username'\]}
  • html 是没用的,除非你将它嵌入到你的 foreach 循环中,并且 \[ 的东西也毫无意义......无论如何你都没有在 html 中回显 php,你是基本上是在做“这是一个行变量”,而 php 会“很好,无论如何”。
  • 对不起,混淆了伙计们,我添加了 html,但这不是必需的。我的问题是如何制作一个表格并添加多个用户名和密码?
  • 请使用 PHP 的built-in functions 来处理密码安全问题。如果您使用的 PHP 版本低于 5.5,则可以使用 password_hash() compatibility pack

标签: php mysql pdo


【解决方案1】:

好的,我知道了

<?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>

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

很抱歉以令人困惑的方式写下我的问题。

【讨论】:

  • 这行不通。 $users != $user 在你的 foreach 循环中,你仍然没有连接。您不得与我们分享整张照片。
【解决方案2】:

所以我已经将名称和内容编辑为我的项目所需的内容,但它仍然几乎相同。我还制作了 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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2015-03-03
    • 1970-01-01
    • 2015-10-21
    • 2011-12-16
    • 1970-01-01
    • 2015-04-22
    相关资源
    最近更新 更多