This question already has an answer here:
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;
</div>
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.