I tried to insert image from its path into mysql database using php and I did it but when I need to retrieve the image from mysql database into tag the image doesn't appear.
my database
CREATE TABLE IF NOT EXISTS `tbl_images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` blob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
index.php
<?php
$connect = mysqli_connect("localhost", "root", "", "test");
if(isset($_POST["insert"]))
{
$path='C:\\xampp\\htdocs\\test_img\\4.jpg';
$query = "INSERT INTO tbl_images(name) VALUES ('LOAD_FILE($path)')";
if(mysqli_query($connect, $query))
{
echo '<script>alert("Image Inserted into Database")</script>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="submit" name="insert" id="insert" value="Insert" />
</form>
<table >
<?php
$query = "SELECT * FROM tbl_images ";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result)) {
echo '
<tr>
<td> <img src="data:image/jpeg;base64,'.base64_encode($row['name'] ).'" height="200" width="200" /> </td>
</tr>
';
}
?>
</table>
</body>
</html>
Edit: the insert doesn't work correctly. it insert the string LOAD_FILE(C:xampphtdocs est_img4.jpg) instead of the image itself.