php - 尝试使用 PDO 语句将结果显示在表上

标签 php mysql

我是 PDO 新手,试图弄清楚如何让它与 PDO 一起工作。我让它与 MYSQL 一起工作,我可能对 PDO 的工作原理感到困惑。我收到一张空白页,有任何关于如何获取收到记录的所有结果的想法。我看到有关 PDO 的教程,但当我这样做时,它是针对带有数组的单个记录。

<?php
require_once("../db_connect.php");

$stmt = $db->prepare ("SELECT * FROM requests WHERE status='Received'");
echo"Received Requests";
echo "<br><br>";

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo("<table  bgcolor=F2F2F2 width=1080 border='2'>");

echo("<br><tr><th>Id</th><th>Update</th><th>LanID</th><th>Name</th><th>Location</th><th>Manager</th><th>request</th><th>Description</th><th>request_comments</th><th>Status</th><th>Comments</th><th>Completed User</th><th>Completed Date</th></tr>");

echo("<tr>");
echo  "<td>". $row['id'] . "</td>"
."<td><a href='../update.php?id="  . $row['id'] .  "'>Update</a></td>"
."<td>" .  $row['lanId'] . "</td> "
. "<td>".  $row['name'] . "</td>"
. "<td>". $row['department'] . "</td>"
. "<td>" . $row['manager'] . "</td>"
. "<td>" . $row['request'] ."</td>"
. "<td>" . $row['request_description'] ."</td>"
. "<td>" . $row['request_comments'] ."</td>"
. "<td>" . $row['status'] ."</td>"
. "<td>" . $row['comments'] ."</td>"
. "<td>" . $row['compUser'] ."</td>"
. "<td>" . $row['compDt'] ."</td>";

echo '</tr>';

}
echo("</table>");

?>

<html>

<head>
 <meta http-equiv="refresh" content="5" >
<title>

</title>
</head>
<body background="../images/background.jpg">
</body>

</html>

db_connect.php

<?php


$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "systems_requests";

$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
?>

最佳答案

您忘记执行准备好的语句。

使用$stmt->fetch()之前请先使用$stmt->execute()

另外请像这样获取:

$requests = $stmt->fetch (PDO::FETCH_ASSOC);

foreach ($requests as $request) {

    echo $request["whateverKeyYouWant"];

}

关于php - 尝试使用 PDO 语句将结果显示在表上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26782138/

相关文章:

mysql - 更改 MySQL 错误消息语言

php - Magmi Import SQL 导入,适用于本地服务器

mysql - 如何获取每个月*at*的注册用户总数?

php - Zend Framework 2 - 如何使用外部库

php - 从 Controller 访问 Liip Imagine bundle - 将服务分配给变量(Symfony 4)

php - Zend 框架 - 如何验证电子邮件?

php - 在mysqli中调用两个存储过程导致 'Commands out of sync'错误

php - 获取表的总行数

javascript - 如何选择和更改按钮的颜色并在单击其他按钮时恢复为原始颜色

mysql - 如何从具有事件记录类codeigniter的mysql中选择最高浮点值