I cannot figure out why my image is not displaying in my index.php
page.
I use PDO to connect and use the database, but I faced some weird problem that has never happened to me before. The image that I stored in my database as blob type is not displaying in my index.php
page.
Here's my code:
<?php
$dsn = 'mysql:host=localhost;dbname=website;charset=utf8mb4';
$pdo = new PDO($dsn, 'root', '');
if ( isset($_POST['upload']) )
{
$file = addslashes(file_get_contents($_FILES['image']['tmp_name']));
if ( !empty($file) )
{
$sql = " INSERT INTO images(name) VALUES (:name) ";
$pdo->prepare($sql)->execute(array('name' => $file));
}
}
?>
This I use to display images on my images div tag:
<div class="images" id="Images">
<?php
$sql = " SELECT * FROM images ";
$result = $pdo->query($sql);
if ( $result->rowCount() > 0 )
{
while ( $row = $result->fetch() )
{
echo '<img src="data:image/jpeg;charset=utf-8;base64,' .base64_encode($row['name']). '" alt="Binary Image"/>';
}
}
?>
</div>