Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am not able to display an image from MySql database.

What I have tried:

<?php
require 'dbconnect.php';

$stmt=$con->prepare("SELECT * FROM destination ORDER BY heading ASC");
$stmt->execute();

$data=$stmt->fetchAll();
foreach($data as $row){
   $heading=$row['heading'];
  $image=$row['picture'];
  //echo '<img src="data:image;base64,'.base64_encode($image).'">';
  echo '<img src="data:image/jpg;base64,'.base64_encode($row['picture']).'" style="width:35px;height:40px;">';
  

}


?>
Posted
Comments
Chris Copeland 17-May-22 15:15pm    
How is the image in the database encoded? Is it a raw binary column or a string column?
Indu B Nath 17-May-22 23:52pm    
if(isset($_POST['dest_save'])){

$imagename=addslashes(file_get_contents($_FILES["dest_picture"]["tmp_name"]));


Richard Deeming 18-May-22 4:41am    
There's not enough code there to see how you're saving the image to the database. But the use of addslashes makes me suspect you're writing code which is vulnerable to SQL Injection[^].

PHP: SQL Injection - Manual[^]

In addition to introducing a critical security vulnerability into your code, trying to save the file contents without using a properly-parameterised query will result in data corruption.
Richard MacCutchan 18-May-22 4:08am    
The src element of the img tag needs to be a path to an actual file.

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