2013-05-13 137 views
-2

希望有人可以給我一些幫助...我使用PDO從數據庫中獲取一些數據,但每次腳本運行時都會返回並且沒有錯誤,但不會顯示所需的數據,我知道我正在尋找的數據也在那裏..任何幫助將不勝感激,這是我目前的,謝謝。PDO選擇語句

$db = new PDO('sqlite:C:\\xampp\\htdocs\\Utils\\PDF_Utils\\PDF2Word\\details.sqlite'); 

    echo "<table border=1>"; 
    echo "<tr><td>FileID</td> 
       <td>File Name</td> 
       <td>Email From</td> 
       <td>CC</td> <td>Subject</td> 
       <td>File Size</td></tr>"; 


       $contents = $db->prepare("SELECT * FROM details WHERE fileName = 
         '$yourFileName'"); 
       $contents->execute(); 

        foreach($contents as $row) { 
         echo "<tr><td>" . $row['FileID'] . "</td>"; 
         echo "<td>" . $row['fileName'] . "</td>"; 
         echo "<td>" . $row['emailFrom'] . "</td>"; 
         echo "<td>" . $row['CC'] . "</td>"; 
         echo "<td>" . $row['subject'] . "</td>"; 
         echo "<td>" . $row['fileSize'] . "</td></tr>"; 
        } 
        echo "</table>"; 

回答

-1

準備語句時使用的佔位符,然後執行查詢傳遞參數,如:

while($row = $contents->fetch(PDO::FETCH_ASSOC)){ 
    //your code here; 
} 
+0

應該是'array($ yourFileName)' – hjpotter92 2013-05-13 10:27:27

+0

@ hjpotter92錯過了。固定。 – Ander2 2013-05-13 10:29:17

+0

試過了,它仍然只是顯示一張空白表... – 2013-05-13 10:34:11

-1

在方括號內:

$contents = $db->prepare("SELECT * FROM details WHERE fileName = ?"); 
$contents->execute(array($yourFileName)); 

然後使用FETCH_ASSOC獲取結果$ yourFileName as {$ yourFileName}

+1

沒有區別 – 2013-05-13 10:29:23

0
$stmt = $db->prepare("SELECT * FROM details WHERE fileName = ?"); 
$stmt->execute(array($yourFileName)); 
$contents = $stmt->fetchAll(); 

其餘的都是一樣的

+0

它做同樣的事情,只是顯示一張空白表 – 2013-05-13 10:30:54

+0

1.它不是一回事。 2.要有非空白的屏幕,你必須[正確設置你的PDO和PHP](http://stackoverflow.com/questions/15990857/reference-frequently-asked-questions-about-pdo#15990858) – 2013-05-13 10:34:24

+0

現在修復它,感謝無論如何幫助,但做了一個不同的方式。 – 2013-05-13 11:02:29