这是我在数据库中插入和显示图像的代码(blob 数据类型),插入每次都成功,但我无法显示任何图像。 而且每次都不知道出了什么问题。
<?php
$hostname = "mysql.hostinger.in";
$username = "u881956746_jk";
$password = "jay@95373";
$dbname = "u881956746_quote";
$conn = mysqli_connect($hostname , $username , $password , $dbname);
if(!$conn)
{
die("Connection Failed : " . mysqli_connect_error());
}
if(isset($_POST['submit']))
{
if(isset($_FILES['file']))
{
$count = count($_FILES['file']['name']);
for($i = 0 ; $i < $count ; $i++)
{
$image = mysqli_real_escape_string($conn , $_FILES['file']['tmp_name'][$i]);
$image = base64_encode($image);
$sql = "INSERT INTO `u881956746_quote`.`quotes` (`Image`) VALUES ('$image');";
if(mysqli_query($conn , $sql))
{
echo "Successfully Inserted";
}
else
{
echo "Not inserted Succesfully";
}
}
}
else
{
echo "Nothing to Submit";
}
}
if(isset($_POST['Show']))
{
$sql = "SELECT * FROM `quotes`";
$result = mysqli_query($conn , $sql);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo '<img height = "300" width = "400" src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/>';
echo "<br />";
}
}
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<body>
<form method = "POST" action = "practice2.php" enctype = "multipart/form-data">
<input type = "file" name = "file[]" id = "image" multiple />
<br />
<br />
<input type = "submit" name = "Show" value = "Show" />
<br />
<br />
<input type = "submit" name = "submit" value = "Submit File" />
</form>
</body>
</html>
最佳答案
从显示图像中删除 base64_encode
,因为它在插入过程中已转换为 base64
if(isset($_POST['Show']))
{
$sql = "SELECT * FROM `quotes`";
$result = mysqli_query($conn , $sql);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo '<img height = "300" width = "400" src="data:image/jpeg;base64,'.$row['Image'].'"/>';
echo "<br />";
}
}
}
关于php - 无法在 PHP 中显示 MySQL 数据库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44645897/