为什么我的图像会在这些奇怪的角色中回响?

时间:2018-01-16 07:40:08

标签: php html sql image phpmyadmin

我使用以下内容将图像插入到我的phpmyadmin数据库中:

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

当我尝试将它回显到我的网页上时,我会看到这些奇怪的角色 click to view photo

我的语法是正确的我确定,如果不是,请纠正我,但我不知道为什么我的图片会将自己从我的数据库转换为这些时髦的角色。

<?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')
?>

2 个答案:

答案 0 :(得分:0)

显示图像(如果存储为blob

echo "<img src='data:image/jpeg;base64, " . base64_decode( $img ) . "' />";

答案 1 :(得分:0)

您将图像存储为数据库中的二进制数据(blob列)。当您显示图像时,您将把二进制数据放入src - 标记的img属性中。

您需要提供图像链接(例如,单独的PHP文件,从数据库查询图像并将其输出到浏览器)或使用正确的数据编码。

额外的php文件示例:

$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];

编码示例:https://stackoverflow.com/a/27696825/1392490