我对这个错误真的很困惑 我已尽一切努力来解决此错误,但我无能为力 有人可以帮我吗
我正在尝试在 php 中显示 SQL 数据库中存储的所有值
<?php
require('includes/config.php');
//if not logged in redirect to login page
if (!$user->is_logged_in()) {
header('Location: login.php');
}
//define page title
$title = 'Members Page';
//include header template
require('layout/header.php');
?>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
<h2>Member only page - Welcome <?php echo $_SESSION['username']; ?></h2>
<p><a href='logout.php'>Logout</a></p>
<hr>
</div>
</div>
<?php
$result = $db->prepare("SELECT * FROM password");
$result->execute();
while ($row = $db->fetch(PDO::FETCH_ASSOC))
{
$title = $row['email'];
$body = $row['pass'];
}
echo "<table>
<tr>
<th>Usernamw</th>
<th>Password</th>
</tr>
<tr>
<td>" . $title . "</td>
<td>" . $body . "</td>
</tr>
</table>" ?>
</div>
<?php
//include header template
require('layout/footer.php');
?>
这是我收到错误的地方
while ($row = $db->fetch(PDO::FETCH_ASSOC))
最佳答案
你混淆了你的变量/对象:
while ($row = $db->fetch(PDO::FETCH_ASSOC))
应该是
while ($row = $result->fetch(PDO::FETCH_ASSOC))
您必须获取结果,而不是连接。
编辑:要让所有行都得到回显,您必须将它们包含在 while
循环中:
echo "<table>
<tr>
<th>Usernamw</th>
<th>Password</th>
</tr>";
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
$title = $row['email'];
$body = $row['pass'];
echo "<tr><td>" . $title . "</td><td>" . $body . "</td></tr>";
}
echo "</table>";
关于php - fatal error :在第 24 行调用 C:\wamp\www\FINAL login\memberpage.php 中未定义的方法 PDO::fetch(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34142488/