Jump to content

selecting longblobs in php

Go to solution Solved by Brenz,

You can't just echo the blob and expect it to work. You need to wrap it in valid HTML as well. Try this:

<img src="data:image/jpeg;base64,<?php echo base64_encode($row['logo']) ?>" alt="My Image" />

 

I'm having difficulties selecting and displaying images i've saved in my database with longblob. there are a lot of examples on google that presumably work but after trying them all, nothing works. 

this is a part of my code: 

$sql = "SELECT teamID, name, date, managerID, points, logo FROM team;";

if(!$result = $mysqli->query($sql)){
    die('There was an error running the query [' . $mysqli->error . ']');
}
echo '<div>';
echo '<table border="1">';
while($row = $result->fetch_assoc())
{
	echo '<tr>
     <td>'.$row['teamID'] . '</td>
	 <td>'.$row['name'] . '</td>
	 <td>'.$row['date'] . '</td>
	 <td>'.$row['managerID'] . '</td>
	 <td>'.$row['points'] . '</td>
	 <td>'.$row['logo'] . '</td>
	 </tr>';
}
echo '</table>';
echo '</div>';

what's wrong and why doesn't it work? I used phpmyadmin to select an image and save it into the database, firstly i connected to it and then wanted to display a table of teams. But as soon as i add blob to the mix it doesn't work. Without it everything works and after that i just get a table with random signs instead of a picture.

What could be the problem?

Link to comment
Share on other sites

Link to post
Share on other sites

You can't just echo the blob and expect it to work. You need to wrap it in valid HTML as well. Try this:

<img src="data:image/jpeg;base64,<?php echo base64_encode($row['logo']) ?>" alt="My Image" />

 

Link to comment
Share on other sites

Link to post
Share on other sites

I would highly suggest against the storing of images in your database, instead, I would suggest you store the filepath to said image. Databases are made for structured data and do not handle images very well, this leads to increased overhead on a file system level (as the database will use more data to store the same images), and a performance decrease. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×