0

I have a page where a user enters an ID for a book record and it is supposed to pull the corresponding image (only the image) that was entered with a books details from the database which is stored as a BLOB. - Yes, its better to store the image file path - I know this, but that's not what i'm trying to achieve here.

Currently when I press the submit button to find an ID specific by user it displays a page with an image symbol, but no image.

Here is my code.

require_once __DIR__.'/config.php';
session_start();
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);

$book = $_POST["book_id"];
$stmt = $dbh->prepare("select image from books2 where b_id = $book ");
$stmt->execute();


if ($stmt) {
    if ($row = $dbh->prepare($stmt)) {
       $img = $row["image"];
    }
}

header("Content-type: image/jpeg");

echo "$img";

How do I get the image to display, I have read other questions but haven't been able to figure it out?

stark
  • 287
  • 4
  • 9
  • 20
  • 3
    You still need to decode your img `echo base64_decode($img);` – Daan Apr 20 '15 at 10:11
  • Okay thanks, done! Still didn't fix the problem though, the image is still not displaying. – stark Apr 20 '15 at 10:13
  • Can you post the BLOB string? – Daan Apr 20 '15 at 10:16
  • Sorry, I don'y know what you mean by blob string. Do you mean the characters represent the blob in the database? i.e. if you do SELECT image from books2? the characters that are stored? – stark Apr 20 '15 at 10:23
  • Yes that is what I mean. – Daan Apr 20 '15 at 10:25
  • If i paste it into a text file it is 44 pages long. Here's a small section: ▒▒?▒,='▒6<▒

    K▒▒▒h▒▒▒<+▒▒▒W▒▒Ap`▒▒▒▒ ▒▒e݌▒▒ʷ▒[+▒▒V▒▒▒b▒▒*▒n▒▒Ȉ▒▒▒▒k▒▒Y▒▒▒a▒Z▒o9 7+▒z▒▒▒▒k|▒▒M▒=ַR&▒j▒lw▒▒ O▒▒▒\?R bW▒▒]▒y▒{▒▒5▒▒|?

    – stark Apr 20 '15 at 10:28
  • 1
    Another method that works: [PHP: Retrieve image from MySQL using PDO](http://stackoverflow.com/questions/5999466/php-retrieve-image-from-mysql-using-pdo) – Ryan Vincent Apr 20 '15 at 10:36
  • Fantastic it works! Thank you! I'll post the answer I got from here. – stark Apr 20 '15 at 10:57

3 Answers3

2

You have an sql injection problem and you are not fetching a row with the image data:

$book = $_POST["book_id"];
$stmt = $dbh->prepare("select image from books2 where b_id = $book ");
$stmt->execute();


if ($stmt) {
    if ($row = $dbh->prepare($stmt)) {
       $img = $row["image"];
    }
}

Should be something like:

$stmt = $dbh->prepare("select image from books2 where b_id = ?");
$stmt->execute(array($_POST["book_id"]));

if ($row = $stmt->fetch()) {
   $img = $row["image"];
}

Also note that you don't have any error handling in case no row is found. You should add that as well.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • Thanks good point about the error handling. Sadly still no luck with displaying the image. – stark Apr 20 '15 at 10:44
  • 1
    @user1830632 At least does `$img` contain the image data and is the image a jpeg? – jeroen Apr 20 '15 at 10:45
  • If I do var_dump($img) it does not return anything. – stark Apr 20 '15 at 10:52
  • @user1830632 Are you outputting more than just the image in your script? You should comment out your `header` so that you can see the exact output. – jeroen Apr 20 '15 at 10:53
  • I tried another method suggested by a user and the code is now working, see answer. Thanks for the help though. – stark Apr 20 '15 at 11:04
1

Instead of adding like this its better to do using the below method.

Just add your code in one file lets call display.php

require_once __DIR__.'/config.php';
session_start();
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);

$book = $_GET["book_id"];
$stmt = $dbh->prepare("select image from books2 where b_id = $book ");
$stmt->execute();


if ($stmt) {
    if ($row = $dbh->prepare($stmt)) {
       $img = $row["image"];
    }
}

header("Content-type: image/jpeg");

echo "$img";

Then using the img tag you can easily print the image by passing the book_id as a url in src

As like below

<img src="display.php?book_id=123" alt="your alt title"  />
1

This is the working code thanks to the help of this question : PHP: Retrieve image from MySQL using PDO suggested by Ryan Vincent.

$book = $_GET["book_id"];
$sql = "SELECT image FROM books2 WHERE b_id=:id";
$query = $dbh->prepare($sql);
$query->execute(array(':id' => $book));

$query->bindColumn(1, $image, PDO::PARAM_LOB);
$query->fetch(PDO::FETCH_BOUND);
header("Content-Type: image");
echo $image;
Community
  • 1
  • 1
stark
  • 287
  • 4
  • 9
  • 20