PDO Select Statement

时间:2013-05-13 10:19:38

标签: php 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>";

3 个答案:

答案 0 :(得分:0)

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

其余的是相同的

答案 1 :(得分:-1)

在准备语句时使用占位符,然后执行传递参数的查询,如下所示:

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

然后使用FETCH_ASSOC获取结果:

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

答案 2 :(得分:-1)

将$ yourFileName括号括起来{$ yourFileName}