0

I inserted an image into my phpmyadmin database using this:

UPDATE
inventory
SET
IMG = LOAD_FILE('A:/Programs/XAMPP/htdocs/SamsCarLot/images/mercedes-benz.jpg')
WHERE
VIN = 'WDDGF8AB9DR298549';

When I try to echo it onto my webpage I get these very odd looking characters click to view photo

My syntax is correct I'm sure, correct me if it isn't, but I don't know why my image is converting itself from my database to these funky characters.

<?php
$vin = mysqli_real_escape_string($conn, $_GET['VIN']);
$sql = "SELECT * FROM inventory WHERE VIN = '$vin'";
$result = $conn->query($sql);
$stmt = $conn->prepare("SELECT Model FROM inventory WHERE VIN = ?");
$stmt->bind_param("s", $vin);
$stmt->execute();
$stmt->bind_result($model);
$stmt->fetch();
echo "<h1>$model</h1>";


// Loop through all the rows returned by the query, creating a table row for each
while ($result_ar = mysqli_fetch_assoc($result)) {
  $img = $result_ar['IMG'];
  $year = $result_ar['YEAR'];
  $make = $result_ar['Make'];
  $model = $result_ar['Model'];
  $trim = $result_ar['TRIM'];
  $color = $result_ar['EXT_COLOR'];
  $interior = $result_ar['INT_COLOR'];
  $mileage = $result_ar['MILEAGE'];
  $transmission = $result_ar['TRANSMISSION'];
  $price = $result_ar['ASKING_PRICE'];
}
echo "<IMG src='$img' width='250'>";
echo "$year $make $model</p>";
echo "<p>Asking Price: $price </p>";
echo "<p>Exeterior Color: $color</p>";
echo "<p>Interior Color: $interior </p>";

$conn->close();
//INSERT INTO images (img) VALUES ('A:/Programs/XAMPP/htdocs/Sam'sCarLot/images/ferrari.jpg')
?>
Ryan
  • 31
  • 7
  • Are you storing images as a blob in the database? – Qirel Jan 16 '18 at 07:41
  • 1
    `LOAD_FILE` reads a file and returns the content as a string - so what you are seeing is basically correct. It is however better to store just the path to the image rather than a blob like this – Professor Abronsius Jan 16 '18 at 07:43
  • Yes as a LONGBLOB – Ryan Jan 16 '18 at 07:44
  • UPDATE inventory SET IMG = LOAD_FILE('A:/Programs/XAMPP/htdocs/SamsCarLot/images/mercedes-benz.jpg') WHERE VIN = 'WDDGF8AB9DR298549'; is the only way I know how to store an image in a database. I'm new to this, I've never used a sql script to store an image before and when I googled how to do it that's the code I got – Ryan Jan 16 '18 at 07:47

2 Answers2

0

To display the image if stored as a blob

echo "<img src='data:image/jpeg;base64, " . base64_decode( $img ) . "' />";
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

You are storing the image as binary data in your database (blob column). When you display your image, you are putting that binary data into the src attribute of your img-tag.

You need to provide a link to the image (e.g. a seperate PHP file, that queries the image from database and outputs it to the browser) or use proper encoding of the data.

Example with extra php file:

$vin = $_GET['vin']; 
$stmt = $conn->prepare("SELECT Model FROM inventory WHERE VIN = :vin");
$stmt->bind_param(":vin", $vin);
$stmt->execute();
$row = $stmt->fetch();

// Output image
header('Content-Type: image/jpeg'); // For jpg-files
echo $row[0];

Example with encoding: https://stackoverflow.com/a/27696825/1392490

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38