无法使用php pdo代码获取我存储在数据库中的内容

时间:2018-02-15 06:29:09

标签: php mysql pdo

嘿我是php的新手,我尝试在数据库中上传图片并从数据库中显示。

问题:

当我尝试获取时,我无法获取特定图像,我总是得到这个“array(0){}”。所以请帮我清除这段代码。谢谢提前。

<?php
include'config.php';

if (isset($_POST["ok"])) {
$folder = "img/";
$image = $_FILES['image']['name'];
$path = $folder.$image;
$target_file=$folder.basename($_FILES['image']['name']);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$allowed = array('jpeg','png','jpg'); 
$filename = $_FILES['image']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);

if (!in_array($ext, $allowed)) {
    echo "sorry only jpg,jpeg,png and gif files are allowed";
}
else{
    move_uploaded_file($_FILES['image']['tmp_name'], $path);
    $sth=$conn->prepare("INSERT INTO sktable(image)VALUES('$image');SELECT 
image FROM sktable WHERE $image");
    $sth->execute();
    echo "<table border='2'>".
"<tr>".

    "<th>"."Image"."</th>".
"</tr>";


$select = $conn->prepare("SELECT * FROM sktable");

$select->execute();
$data=$select->fetchAll(PDO::FETCH_ASSOC);
var_dump($data);
}
}
?>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" name="ok"/>
</form>

<style type="text/css">
#pimg{
    width: 100px;
    height: 100px;
}
</style>

1 个答案:

答案 0 :(得分:0)

在循环中使用while循环和打印变量。

 <?php
    $select = $conn->prepare("SELECT * FROM sktable");
    $select->execute();
    while ($row = $select->fetch(PDO::FETCH_ASSOC)){
       print_r($row);
    } 
    ?>

否则您也可以使用foreach循环

<?php
 $select = $conn->prepare("SELECT * FROM sktable");
 $select->execute();

foreach ($select->fetchAll(PDO::FETCH_ASSOC) as $row) {

}

?>