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 ;
There are a few issues here and I'm unsure if those curly quotes as I stated in comments are part of your code or not, including the "t" in );t
<= delete that "t" if it's part of your code.
If so, ‘ ’
and “ ”
need to be changed to '
and "
respectively.
However, your connection is using $dbh
, and you're using $con
in your query.
$dbh = new PDO('mysql:host=localhost;dbname=probe', $root);
^^^^ ^^^^^ unknown
and the query:
$stmt = $con->prepare( $query ); // minus the "t" of course.
^^^^
However, $root
is unknown and not posted.
Check for errors:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Also add $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
right after the connection is opened.
$dbh
or $con
, depending on which variable you are using.Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments