2014-11-06 81 views
0

我是新來的PDO試圖找出如何得到它與PDO一起工作。我有它與MYSQL合作我可能困惑PDO如何工作。我對如何獲取所有收到的記錄結果有了一個空白頁。我看到關於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); 
 
?>

+1

得到任何錯誤?我也想看到你的db_connect文件。 – 2014-11-06 14:47:18

+0

安裝/啓用了「PDO」擴展嗎?你有沒有做過任何調試?你的錯誤日誌裏有什麼? – 2014-11-06 14:48:41

+0

將第一個表標記放在while循環之外。在準備之後,您還缺少執行 – Mihai 2014-11-06 14:52:01

回答

0

你忘了執行你事先準備好的聲明。

請使用$stmt->execute()使用$stmt->fetch()

之前也請取像這樣:

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

foreach ($requests as $request) { 

    echo $request["whateverKeyYouWant"]; 

} 
+0

我能夠通過添加$ stmt-> execute();提取之前 – Donny 2014-11-06 17:05:26

+0

唯一的辦法就是我寫表的方式是做一個正確的方法,它可以安全地從sql注入。任何人都可以提供一個正確的方法來做到這一點。 – Donny 2014-11-06 17:14:49

+0

@Donny [這裏是一個鏈接](http://php.net/manual/en/pdostatement.bindparam.php)以正確的方式向查詢添加參數,很高興看到它現在正在工作。快樂的編碼! :) – Vanitas 2014-11-06 17:18:41