I'm trying to display a picture from mysql database, but somehow it isn't working. The picture is in jpeg format. I use a simple php code with a database connect file and a source file. The weird thing is that if I want to download the picture directly from the database, it stays as a binary .bin file. Here are my codes:
db_connect.php:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=probe', $root);
?>
source.php:
<?php
include ‘db_connect.php’;
$query = “select * from users”;
$stmt = $con->prepare( $query );t
$stmt->bindParam($_GET['image']);
$stmt->execute();
$num = $stmt->rowCount();
if( $num ){
$row = $stmt->fetch(PDO::FETCH_ASSOC);
header(“Content-type: image/jpeg”);
print $row['image'];
exit;
}else{
//if no image found with the given id,
//load/query your default image here
}
?>
And in the index file I just call the source.php to display the picture:
<img src="../reg/source.php" >
This is the SQL source:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image` blob NOT NULL,
`image_type` varchar(100) NOT NULL,
`image_size` varchar(100) NOT NULL,
`image_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=44 ;