我是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->execute ()之前,请先使用$stmt->fetch ()

还请这样获取:

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

foreach ($requests as $request) {

    echo $request["whateverKeyYouWant"];

}

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

10-14 13:57
查看更多