Reputation: 13
i am trying to use mysql database and php to display a image in a table. Every other information in my table is displaying fine except for image, i am receiving this.
����JFIF��� ( %!1!1)....383,7*-.7 +%&-----5---+--------5-----+-------------------5-/---����"����B!1A"Qa2
In my table and this goes on for awhile. in my database i have images set to a blob and no null. I am using this code to display it.
<?php
include 'connect.php';
$results = mysqli_query($con, 'select * from products');
?>
<table cellpadding="3" cellspacing ="3" border="0">
<tr>
<th>Image</th>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Buy Product</th>
</tr>
<?php
while($products= mysqli_fetch_object($results)){
if($products->id<=10){?>
<tr>
<td><?php echo $products->images;?></td>
<td><?php echo $products->name;?></td>
<td><?php echo $products->description;?></td>
<td><?php echo $products->price;?></td>
<td><a href="cart.php?id=<?php echo $products->id; ?
>">Add to Cart</a></td>
</tr>
<?php }} ?>
</table>
Any help would be appreciated. Thank you.
Upvotes: 1
Views: 947
Reputation: 4411
First I have to say that storing image data in the database can be a bad idea especially when you have a lot of images. A slightly better approach might be to store just the path to the file and link to it in your html code.
Anyway, you have a problem and there is a solution. At the moment you are just displaying the BLOB
contents, which is the raw binary data of the image. To display an image, you need an <img>
tag with the right source.
Something like this:
<img src="data:image/jpeg;base64,<?= base64_encode($blob) ?>"/>;
In your exact case it will look more like this:
...
<td>
<!-- JPG image -->
<img src="data:image/jpeg;base64,<?= base64_encode($products->images) ?>"/>;
</td>
...
Upvotes: 1
Reputation: 31
Storing images in database is generally considered a bad idea, you should try to store the image path instead.
If you really need to do it that way, you need to use the correct tags to display them :
<td><img alt="" src="data:<?php echo $products->images;?>"></td>
For example, but please reconsider.
Upvotes: 0