I want to display images from my MySQL database to an website that I'm working on but when I visit the site I see just a box with a broken image or lot of symbols.
In my MySQL database the image type is LONGBLOB
(MIME: image/jpeg).
This is the code that I'm using (I have HTML codes before the PHP codes so I'm using a buffer).
<?php
mysql_connect("localhost", "root", "") or die('connection error');
mysql_select_db("produse");
$per_page = 25;
$pages_query = mysql_query("SELECT COUNT('id') FROM filtrederetea");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($GET['page'])) ? (int)$GET['page'] : 1;
$start = ($page -1) *$per_page;
$query = mysql_query("SELECT * FROM filtrederobinet");
echo "<table>";
if(isset($_GET["id"]))
{
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * FROM filtrederetea");
while ($query_row = mysql_fetch_assoc($query)) {
$imagedata = $query_row['img'];
'<img src="data:image/jpeg;base64,' . base64_encode($query_row['img']);
};
header('Content-Type: image/jpeg');
echo "($imagedata)";
}
else
echo "error";
while ($query_row = mysql_fetch_assoc($query)) {
echo "<tr>";
echo "<td>";
echo.$query_row["img"];
echo "</td>";
echo "<td>";
?>
<img src=<?php echo $query_row["img"]; ?> width="130" height="130">
<?php
echo "</td>";
echo "</tr>";
}
echo "</table>";
if ($pages >=1) {
for ($x=1;$x<=$pages;$x++) {
echo '<a href="?page'.$x.'">'.$x.'</a> ';
}
}
ob_end_flush()
?>
header('Content-Type: image/jpeg');
– Ismael Miguel Sep 12 at 15:59mysql_query
is an obsolete interface and should not be used in new applications and will be removed in future versions of PHP. A modern replacement like PDO is not hard to learn. If you're new to PHP, a guide like PHP The Right Way can help explain best practices. – tadman Sep 12 at 16:11