Daniyal Saeed Daniyal Saeed - 8 months ago 28
MySQL Question

Display Image on website from mysql database via php

I can't find any syntax or logical error in this code, but i don't why the image is not being displayed on the web page, also it is not showing any error,i'm sharing the code and the output that i'm getting.
If you can help me in displaying image, i'll be very thankful to you.

Output on the Web Page


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "CSE";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);

$query = "SELECT * FROM upload WHERE id=1";
$result = $conn->query($query);
while($row = $result->fetch_assoc())

header("Content-Type: image/png");
echo '<img height="300" width="300" alt="logo" src="data:image;base64,'.$row["name"].'">';




First, remove the header. You are outputting HTML, not just an image + you already have sent HTML to the browser so you can't send any additional headers after that.

Then you need to convert the blob data (binary data) to base64:

while($row = $result->fetch_assoc())
    echo '<img height="300" width="300" alt="logo" src="data:' . $row["type"] . ';base64,'.base64_encode($row["name"]).'">'; 

That's for showing the image inline on the same page.

Edit: To make it more dynamic, I also added the "type" from the database. (since you have it saved).