Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to show the image displayed in database but it shows default image

PHP
<?php
      $link = new PDO("mysql:host=localhost;dbname=campusdrive", "root", "");
      $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $Sql = "SELECT Image FROM student_form WHERE Sessionid = :Sessionid";
      $stmt = $link->prepare($Sql);
      $stmt->bindParam(":Sessionid", $sessionid, PDO::PARAM_STR);
      $sessionid = session_id();
      $stmt->execute();
      $count = $stmt->fetchAll();
      if($users = $stmt->fetchColumn()){         
      echo '<img src="data:image; base64,'.base64_encode($count['Image']).'" style="margin-top: 100px;width: 100px; height: 100px; border-radius: 50px;">';
      } else {
      echo '<i class="fa fa-user-circle" style="font-size: 50px;"></i>';
      }
  ?>


What I have tried:

i tries to display the image form database and i have displayed. i tried in another way such as if the image is not present in database it must show default image, but the problem here is the image is available in database but it shows default image.
Posted
Updated 4-Aug-21 22:51pm

1 solution

That's not a valid data URI:
Data URLs - HTTP | MDN[^]

You need to specify the correct MIME type for the image - eg: image/jpeg, image/png, etc. You have only specified "image", which is not a valid MIME type.

There also shouldn't be a space between the semi-colon and base64,.

Also, fetchAll returns an array containing all of the rows returned by the query. You want to fetch the value of the first column of the first row, which you've already done with fetchColumn.

Try:
PHP
$stmt->execute();
if($image = $stmt->fetchColumn()){         
    echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'" style="margin-top: 100px;width: 100px; height: 100px; border-radius: 50px;">';
} else {
    echo '<i class="fa fa-user-circle" style="font-size: 50px;"></i>';
}
 
Share this answer
 
Comments
Suraj TheDeveloper 5-Aug-21 8:03am    
It worked, thanks so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900