PHP: Retrieve image from MySQL using PDO
I am refactoring some old code, including rewriting basic mysql queries to use PDO.
The follo开发者_C百科wing works brilliantly in all browsers and for all image types:
$query = 'SELECT image FROM image WHERE imageid=' . $image_id;
$result = mysql_query($query, $db_conn); querycheck($result);
header("Content-type: image");
echo mysql_result($result, 0);
Unfortunately, however I rewrite it using PDO, it doesn't work. I've been through the entire PDO documentation and the standard web search, but none of the advice/solutions work.
How can one easily fetch and image from MySQL using PDO and display it?
Edit 1:
Matthew Ratzloff gives what should be the obvious answer below, but it does not work. Here is the actual code that I test using PDO (and I have tried many variants/parameters):
$connectstring_temp = 'mysql:host=' . $A . ';dbname=' .$B;
$dbh_temp = new PDO($connectstring_temp, $login, $password);
#$dbh_temp->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
#$dbh_temp->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
$sql = "SELECT image FROM image WHERE imageid=" . $image_id;
$query = $dbh_temp->prepare($sql);
$query->execute();
$query->bindColumn(1, $image, PDO::PARAM_LOB);
$query->fetch(PDO::FETCH_BOUND);
header("Content-Type: image");
echo $image;
I've kept the same syntax, although for the final code $image_id needs to be passed as a parameter. The code above does NOT work. PDO works fine for all other queries of all types.
You need to paramaterize the imageid value and bind the parameter to PDO::PARAM_LOB
:
$sql = "SELECT image FROM image WHERE imageid=:id";
$query = $db_conn->prepare($sql);
$query->execute(array(':id' => $image_id));
$query->bindColumn(1, $image, PDO::PARAM_LOB);
$query->fetch(PDO::FETCH_BOUND);
header("Content-Type: image");
echo $image;
Of course, you'll also want to specify the complete, correct content type (e.g., image/png).
My personal approach to Image Blobs is using a php file to serve the image, in combination with a GET argument... It's 100% effective and it doesn't involve file creation... For instance, the file to serve the image from the blob would be:
image.php:
<?php
$db = new PDO('mysql:host=localhost;dbname=anything; charset=utf8', 'account','password');
if (isset($_GET['imageid'])) {
$result = $db->prepare("SELECT image FROM image where imageid = ?");
if ($result->execute(array($_GET['imageid']))) {
$row=$result->fetch();
echo $row['pic']; //this prints the image data, transforming the image.php to an image
}
} // you can put an "else" here to do something on error...
?>
This can be called from you main script... For instance:
<?php
$db = new PDO('mysql:host=localhost;dbname=anything; charset=utf8', 'account','password');
//... some code to do your job, where you get your imageid from
$imageid=...
?>
<img src="./image.php?imageid=<?php echo $imageid;?>">
You can use this code to get image from database using PDO:
public function getImage($id){
$sql = "SELECT * FROM images WHERE id = ?";
$sth = $this->dbh->prepare($sql);
$sth->bindParam(1,$id);
$sth->execute();
$num = $sth->rowCount();
if( $num ){
$row = $sth->fetch(PDO::FETCH_ASSOC);
header("Content-type: ".$row['type']);
print $row['image'];
exit;
}else{
return null;
}
}
type - data type(column name)
such as image/png or image/gif
image - image data(column name)
stored in table as LOB
$this->dbh
connection handler
It works for me but now I need to find out how to use it with JS because result of this function is passed to JavaScript code - so called ajax
This code displays all the images in the dabase so you can change it and make it do what you want it to do
getMessage(); } ?><?php
#the folder where the images are saved
$target = "image_uploads/";
$image_name = (isset($_POST['image_name']));
$query = ("SELECT user_id ,image_name FROM tish_images");
$image_show= $con-> prepare($query);
$image_show->execute();
while($record =$image_show->fetch(PDO::FETCH_ASSOC)) {
#this is the Tannery operator to replace a pic when an id do not have one
$photo = ($record['image_name']== null)? "me.png":$record['image_name'];
#display image
echo '<img src="'.$target.$photo.'">';
echo $record['user_id'];
}
?>
精彩评论