How to Display Image from Database Using PHP

How to display images from a database using PHP?

Correction :

$sql=mysqli_query($con,"select * from images");
while($row=mysqli_fetch_array($sql)){
echo "<img src='".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image
}

Also you need to get the actual path via __FILE__ in case if needed.

How to display images from a database in php using option and select

First create dynamic class and set background-image in class then apply class in option tag

<div class="form-group col-md-6">
<label for="inputtype">Game Type</label>
<select class="custom-select mr-md-2" name="image_file">
<option selected></option>
<?php foreach($games as $image): ?>
<script>
.image-option<?php echo $image->image_path(); ?>{
background-image: '<?php echo $image->image_path(); ?>';
width:"40";
height="40";
}
</script>
<option class='image-option<?php echo $image->image_path(); ?>' value="<?php echo $image->region ?>"></option>
<?php endforeach; ?>
</select>
</div>

How to retrieve image from database and display image on the web page

you can use this code to retrieve image from database

<?php
include 'connection.php'
?>
<?php

$result = mysql_query("SELECT * FROM table") or die(mysql_error());

?>

<table border="1" cellpadding="5" cellspacing="5">
<tr> <th>Image</th></tr>

<?php

while($row = mysql_fetch_array($result)) {

$id = $row['id'];

?>
<tr>

<td><img src="uploads/<?php echo $row['pic_name'];?>" alt=" " height="75" width="75"></td>

</tr>

<?php
}
}

?>
</table>

Display image from MySQL using PHP not working

Store and upload image in folder code

<?php

if(isset($_POST['but_upload'])){

$name = $_FILES['file']['name'];
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);

// Select file type
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Valid file extensions
$extensions_arr = array("jpg","jpeg","png","gif");

// Check extension
if( in_array($imageFileType,$extensions_arr) ){

// Insert record
$query = "insert into images(name) values('".$name."')";
mysqli_query($con,$query);

// Upload file
move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);

}

}
?>

<form method="post" action="" enctype='multipart/form-data'>
<input type='file' name='file' />
<input type='submit' value='Save name' name='but_upload'>
</form>

Show in display view.

<?php

$sql = "select name from images where id=1";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);

$image = $row['name'];
$image_src = "upload/".$image;

?>
<img src='<?php echo $image_src; ?>' >

Display image from database in Php page

You forgot wrapping quotes, comma and you don't echo $row['image']. Change:

<img src= data:image/png;base64 ' . $row['image']; .  '  >

to:

<img src="data:image/png;base64,<?= $row['image'] ?>"  >

or (coherently with your global syntax) to:

echo '<img src="data:image/png;base64,' . $row['image'] . '">';

Edit:

I see the pastebin in your last comment. First of all, I think it is not the HTML source, but it is the source from page inspector (after DOM rendering). If you look at the code, you can see that there is not any <img> tag. Your rendered code is:

<div id="answers">/9j/(...)/2Q=="><h2>
└────────────┘
base64 image

The base64 encoded image is correct (as you can see here), but it is not wrapped by correct <img> tag:

<img src="data:image/png;base64,/9j/(...)/2Q==">
└────────────┘
base64 image

Seeing your code:

echo "<table>";
while($row=mysqli_fetch_array($res)) {
echo '<img src="data:image/png;base64,' . $row['image'] . '">';
}
echo "</table>";

Even if <table><img></table> is not valid HTML, I think that the problem is not in your HTML, but it is in your javascript, probably inside the getQuestion() function: test it deeply to retrieve corrected code.



Related Topics



Leave a reply



Submit