Php 使用PDO将结果打印到表中
我正在使用PDO,并试图将结果打印到表上,但这些值不会出现。我需要使用Php 使用PDO将结果打印到表中,php,pdo,html-table,Php,Pdo,Html Table,我正在使用PDO,并试图将结果打印到表上,但这些值不会出现。我需要使用标记 以下是我在francisco帮助下的完整代码: <?php require_once('config.php'); $stmt = $connect->prepare("SELECT id, username, email FROM user"); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_AS
标记
以下是我在francisco帮助下的完整代码:
<?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>
身份证件
用户名
电子邮件
您需要在每次迭代中调用fetchAll()
循环外或调用fetch()
,如下所示:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
// do sth
如果您想要foreach,只需调用
fetchAll()
并对结果执行foreach循环。生成的html是什么?我将深入研究它,到目前为止,如果您想要获得每个用户,它是fetchAll()
而不是fetch()
。你有?在包含config.php之前执行此操作,并查看错误说明。