0

hey I am new to php and i try to upload image in database and display from database.

Issue:

When i try to fetch i am not be able to fetch the particular image always i got this "array(0) { }" .so please help me clear this code .thanks in advance.

<?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>
  • Since you used `fetchAll()`, if you want to display all the data fetched, you need to use `foreach` and not `var_dump` – Carl Binalla Feb 15 '18 at 06:31

1 Answers1

0

Use while loop and print variable inside the loop.

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

else you can also use foreach loop

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

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

}

?>
prakash tank
  • 1,259
  • 1
  • 9
  • 15