HY!
我想列出我桌子的所有行,但它不起作用:(
我的function.php:
function getCharacterAdminJails($account_id) {
global $connection;
$stmt = $connection->pdo->prepare("SELECT * FROM adminjails WHERE jailed_accountID = :account_id");
$stmt->bindParam(":account_id", $account_id);
$stmt->execute();
$adminjail_data = $stmt->fetch(PDO::FETCH_ASSOC);
return $adminjail_data;
}
我的example.php,我列出了我的行:
<table class="table table-hover">
<tr>
<th>Admin neve:</th>
<th>Indok:</th>
<th>Perc:</th>
<th>Időpont:</th>
</tr>
<tr>
<th><?=$adminjail_data["jailed_admin"];?></th>
<th><?=$adminjail_data["jailed_reason"];?></th>
<th><?=$adminjail_data["jailed_ido"];?></th>
<th><?=$adminjail_data["jailed_idopont"];?></th>
</tr>
</table>
如何列出表格的所有行?
答案 0 :(得分:0)
尝试以下代码,通过迭代$adminjail_data
数组列出所有行:
<table class="table table-hover">
<tr>
<th>Admin neve:</th>
<th>Indok:</th>
<th>Perc:</th>
<th>Időpont:</th>
</tr>
<?php foreach($adminjail_data as $row) :?>
<tr>
<th><?=$row["jailed_admin"];?></th>
<th><?=$row["jailed_reason"];?></th>
<th><?=$row["jailed_ido"];?></th>
<th><?=$row["jailed_idopont"];?></th>
</tr>
<?php endforeach; ?>
</table>
答案 1 :(得分:0)
我建议你改变你的php函数中的这行代码来自
$adminjail_data = $stmt->fetch(PDO::FETCH_ASSOC);
对此:
$adminjail_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
由于fatchAll将返回查询中所有结果的数组,然后在您的html页面中执行此操作:
<table class="table table-hover">
<tr>
<th>Admin neve:</th>
<th>Indok:</th>
<th>Perc:</th>
<th>Időpont:</th>
</tr>
<?php foreach($adminjail_data as $row) :?>
<tr>
<th><?=$row["jailed_admin"];?></th>
<th><?=$row["jailed_reason"];?></th>
<th><?=$row["jailed_ido"];?></th>
<th><?=$row["jailed_idopont"];?></th>
</tr>
<?php endforeach; ?>
</table>
这是关于fetchAll的PHP.net文档:
PDOStatement::fetchAll — Returns an array containing all of the result set rows
您可以在此处参考有关PDO fetchAll的完整php文档:https://secure.php.net/manual/en/pdostatement.fetchall.php