I am refactoring some old code, including rewriting basic mysql queries to use PDO.
The following works brilliantly in all browsers and for all image types:
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