简体   繁体   中英

how to fetch all the images from the database

I have a database table where I have stored the images name along with the StudentId. I am fetching the images from a CURRENT student by it's studentId just like an Instagram home page where you can see all your uploaded images. I am not storing the images into a database but saving all the images into a directory called "upload/" and only the name of the images are getting saved into the db.

I have currently 3 images in my db with the same StudentId. But The problem is sometimes I am able to fetch only one image or none from it. I have tried many code but it's still not working. I am not sure where is the mistake. Is it in looping or i am unable to fetch all the images from the db.

<?php
session_start();
include 'common.php';
$id = $_SESSION['student'];
$command = "SELECT * FROM UsersImages WHERE StudentId = ".$id;
$stmt = $dbh->prepare($command);
$result = $stmt->execute();
?>
<html>
   <head>
      <meta charset="UTF-8">
      <title>hello</title>
   </head>
<body>
   <nav>
      <ul>
         <li><a href="home.php">Home</a></li>
         <li><a href="profile.php">Upload</a></li>
      </ul>
   </nav>
   <div>
     <?php
     $row = $stmt->fetch();
     $dir = $row['img_name'];
     foreach($dir as $images){
     echo "<img src=". $images." alt='images'>";
     }
     ?>
   </div>
</body>
</html>

just update your php code, you only store only image name which wrong process so it will not work and use fetch_assoc() to process your array data

<?php
     $row = $stmt->fetch();
     $dir = $row['img_name']; // this is wrong process
     foreach($dir as $images){
     echo "<img src=". $images." alt='images'>";
     }
     ?>

to

<?php
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); // get all result using this
foreach ($results as $data)
{
    $images = $data['img_name']; // get image then use it in your code
    echo "<img src=". $images." alt='images'>";
}
?>

Try this

$results = $stmt->fetch(PDO::FETCH_ASSOC); // get all result using this
    $images = $results['img_name']; // get image then use it in your code
    echo "<img src=". $images." alt='images'>";

for more information about select data with loop

https://www.w3schools.com/php/php_mysql_select.asp

Please find the complete answer for this.

  • Add PDO::FETCH_ASSOC while fetching the data
<?php
session_start();
include 'common.php';
$id = $_SESSION['student'];
$command = "SELECT * FROM UsersImages WHERE StudentId = ".$id;
$stmt = $dbh->prepare($command);
$result = $stmt->execute();
?>
<html>
   <head>
      <meta charset="UTF-8">
      <title>hello</title>
   </head>
<body>
   <nav>
      <ul>
         <li><a href="home.php">Home</a></li>
         <li><a href="profile.php">Upload</a></li>
      </ul>
   </nav>
   <div>
     <?php
     $images = $stmt->fetchAll(PDO::FETCH_ASSOC);
     foreach($images as $image){
     echo "<img src=". $image['img_name']." alt='images'>";
     }
     ?>
   </div>
</body>
</html>

Hope this helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM