Retrieve Image in php



In this tutorial ,we will learn how to retrieve the image from php and pdo.

First ,we need to create two empty files for php

  • dbconfig.php
  • fetchimg.php

CREATE FILE dbconfig.php:



<?php

    $hostname = "localhost";
    $username = "root";
    $password = "";

    try {
        $conn = new PDO("mysql:host=$hostname;dbname=databasename", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        //echo "Database connected successfully";
    } catch(PDOException $e) {
        echo "Database connection failed: " . $e->getMessage();
    }

?>



CREATE FILE fetchimg.php


<?php
include 'dbconfig.php';
$result = $conn->prepare("SELECT images FROM images ORDER BY id DESC"); 
$result->execute();
$fetch = $result->fetchAll(PDO::FETCH_ASSOC);

?>



<div class="container">
<div class="card">
<div class="row col-md-8 col-md-6">
<div class="card-body">
<?php foreach($fetch as $row){?>
<div class="grid grid-view">
<figure>
<a href="<?php echo $row['images']; ?>">
<img class="img-thumbnail rounded mx-auto d-block" src="<?php echo $row['images']; ?>" alt="" /></a>
</figure>
</div>
<?php  } ?>
</div>
</div>
</div>
</div>


Post a Comment

Previous Post Next Post