PHP Code Display Image From Database - Php
How to display image from Mysql database using PHP?
Snippet Code
The below code is used to display image from MySQL database. You have to set image column variable as "blob" for your table.
Create Table:
CREATE TABLE IF NOT EXISTS `images` (
`id` int(11) NOT NULL,
`image` blob NOT NULL
)
Upload and Display image:
<?php
$conn = mysql_connect("localhost","root","");
$dbcon = mysql_select_db("test_db");
if(isset($_POST['sbmit']))
{
$image = addslashes(file_get_contents($_FILES['img']['tmp_name']));
//you keep your column name setting for insertion. I keep image type Blob.
$query = "INSERT INTO images (id,image) VALUES('','$image')";
$qry = mysql_query($query);
}
$sql = "SELECT * FROM images WHERE id = 1";
$qry1=mysql_query($sql);
$result=mysql_fetch_array($qry1);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
?>
<form name='imgdb' method="post" action="" enctype="multipart/form-data">
<input type='file' name='img'>
<input type='submit' value='submit' name='sbmit'>
</form>
Tags