通过php从mysql数据库在网站上显示图像

时间:2016-10-21 18:32:28

标签: php mysql

I can't find any syntax or logical error in this code, but i don't why the image is not being displayed on the web page, also it is not showing any error,i'm sharing the code and the output that i'm getting. If you can help me in displaying image, i'll be very thankful to you.

Output on the Web Page

<html>
<body>
<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "CSE";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$query = "SELECT * FROM upload WHERE id=1";
$result = $conn->query($query);
while($row = $result->fetch_assoc())
{

   header("Content-Type: image/png");
   echo '<img height="300" width="300" alt="logo" src="data:image;base64,'.$row["name"].'">'; 
}
$conn->close();


?>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

首先,删除标题。您正在输出HTML,而不仅仅是图片+您已经将HTML发送到浏览器,因此您无法在此之后发送任何其他标头。

然后你需要将blob数据(二进制数据)转换为base64:

test.loc[(test['injury'] == 'A') & (test['crash_drinking'] == 1) & (test['crash_drugs'] == 0)]
test.loc[(test['injury'] == 'A') & (test['crash_drinking'] == 0) & (test['crash_drugs'] == 1)]
test.loc[(test['injury'] == 'A') & (test['crash_drinking'] == 1) & (test['crash_drugs'] == 0) & (test['driver_drinking'] == 1) & (test['driver_drugged'] == 0)]

用于在同一页面上内嵌显示图像的内容。

修改:为了让它更具动感,我还添加了&#34;类型&#34;来自数据库。 (因为你保存了它。)