0

I was using mysqli to retrieve a blob image from MySQL, but I've been moving everything over to PDO. As far as I can tell, this should be working. I've searched and searched the forums for the answer, but I can't see why this doesn't work.

// Connection
include 'pdo_db.php';

// Get Image Id for DB Query
$recipe = $_GET['recipe'];

// Execute Query<br>
$query = $db->prepare("SELECT * FROM myrecipes WHERE Id = ?");
$query->execute(array(
    "Id" => $recipe
));

// Display Image
$query->bindColumn(1, $image, PDO::PARAM_LOB);
$query->fetch(PDO::FETCH_BOUND);
header("Content-Type: image/jpg");
echo $image;
Pang
  • 9,564
  • 146
  • 81
  • 122
Pez
  • 1
  • 4

1 Answers1

-1

I make two assumptions: 1) your id is numeric integer, and 2) you'd only need to retrieve 1 row at a time (1 per id).

$recipe = (int)$_GET['recipe'];

$query = $db->prepare("SELECT * FROM myrecipes WHERE Id = ? LIMIT 1");
$query->execute(array($recipe));

You are referencing the array in execute as an [id]=>$recipe but this is not needed. You only need as a minimum a numeric reference with is handled automatically.

You should not do SELECT * but instead specify only the column you want, rather than multiple table-columns.

Martin
  • 22,212
  • 11
  • 70
  • 132
  • Correct on your assumption. It's to pull that one image from that one row, with that recipe ID. I've updated the script with your suggestion, and still at the same place where it's not displaying the image? I'm using the core code from this example in SO: http://stackoverflow.com/questions/5999466/php-retrieve-image-from-mysql-using-pdo?rq=1 – Pez Sep 25 '16 at 12:20
  • instead of `SELECT *` you should specify the column name, for the data. – Martin Sep 25 '16 at 12:25
  • Thanks. Rookie mistake :/ +1 from a newbie. – Pez Sep 25 '16 at 12:30
  • @Pez glad you found it `:-)` – Martin Sep 25 '16 at 12:34