从数据库中检索base64图像 [英] retrieve base64 images from database
本文介绍了从数据库中检索base64图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个简单的表格,并以 base64 作为 TEXT 存储了一个图像,当我尝试从数据库中获取所有数据时,它运行良好,除了图像显示在网站上,如下代码所示.
LzlqLzRBQVFTa1pKUmdBQkFRQUFBUUFCQUFELzJ3QkRBQkFMREE0TUNoQU9EUTRT ..etc
php 代码.
<头><元字符集=utf-8"><标题>欢迎<脚本><style></style>头部><身体><表格边框=实心"><tr><th>id </th><th>军用数字</th><th>Fname </th><th>Lname </th><th>图像</tr><?php$conn = mysqli_connect("localhost", "id2549459_salamnti", "0000000000", "id2549459_tutorial");if($conn -> connect_error){die("我的连接失败".$conn -> connect_error);}$sql = "SELECT id, militry_num , firstname, lastname, image from student";$result = $conn->查询($sql);if($result -> num_rows > 0){while($row = $result-> fetch_assoc()){回声<tr><td>".$row["id"] .</td><td>".$row["militry_num"] .</td><td>".$row["firstname"] .</td><td>".$row["lastname"] .</td><td>".base64_encode($row["image"]) ."</td> </tr>";}echo "</table>";}别的{回声 "0 结果" .$conn->错误;}$conn ->关闭();?></html>
任何解决方案如何为我当前的问题显示来自 db 的图像?
解决方案
在 data:image/gif;base64
中解码您的图像字符串<块引用> <img src="data:image/gif;base64,' . $row["image"] . '"/>
<元字符集=utf-8"><标题>欢迎<脚本><style></style>头部><身体><表格边框=实心"><tr><th>id </th><th>军用数字</th><th>Fname </th><th>Lname </th><th>图像</tr><?php$conn = mysqli_connect("localhost", "id2549459_salamnti", "0000000000", "id2549459_tutorial");if($conn->connect_error){die("我的连接失败".$conn->connect_error);}$sql = "SELECT id, militry_num , firstname, lastname, image from student";$result = $conn->query($sql);if($result->num_rows>0){while($row = $result->fetch_assoc()){回声<tr><td>".$row["id"] .</td><td>".$row["militry_num"] .</td><td>".$row["firstname"] .</td><td>".$row["lastname"] .</td><td>".'<img src="data:image/gif;base64,' . $row["image"] .'"/>'."</td> </tr>";}echo "</table>";}别的{回声 "0 结果" .$conn->错误;}$conn->close();?>
I have created simple table and stored an images with base64 as a TEXT , when I tried to fetch all data's from database, it works well, Except the images is displaying on website like below code.
LzlqLzRBQVFTa1pKUmdBQkFRQUFBUUFCQUFELzJ3QkRBQkFMREE0TUNoQU9EUTRT ..etc
php code.
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title> Welcome </title>
<script> </script>
<style></style>
</head>
<body>
<table border = "solid">
<tr>
<th> id </th>
<th> Millitary number </th>
<th> Fname </th>
<th> Lname </th>
<th> Image </th>
</tr>
<?php
$conn = mysqli_connect("localhost" , "id2549459_salamnti" , "0000000000" , "id2549459_tutorial");
if($conn -> connect_error){
die("my connection faild" . $conn -> connect_error);
}
$sql = "SELECT id, militry_num , firstname, lastname,image from students";
$result = $conn-> query($sql);
if($result -> num_rows > 0){
while($row = $result-> fetch_assoc()){
echo "<tr> <td>" . $row["id"] . "</td> <td>"
. $row["militry_num"] .
"</td> <td>". $row["firstname"] .
"</td><td>" . $row["lastname"] . "</td><td>" .
base64_encode($row["image"]) .
"</td> </tr>";
}
echo "</table>";
}
else{
echo "0 result" . $conn->error;
}
$conn -> close();
?>
</table>
</body>
</html>
any solution how do display the images from db for my current problem?
解决方案
Decode your image string in data:image/gif;base64
<img src="data:image/gif;base64,' . $row["image"] . '" />
<head>
<meta charset = "utf-8">
<title> Welcome </title>
<script> </script>
<style></style>
</head>
<body>
<table border = "solid">
<tr>
<th> id </th>
<th> Millitary number </th>
<th> Fname </th>
<th> Lname </th>
<th> Image </th>
</tr>
<?php
$conn = mysqli_connect("localhost" , "id2549459_salamnti" , "0000000000" , "id2549459_tutorial");
if($conn->connect_error){
die("my connection faild" . $conn->connect_error);
}
$sql = "SELECT id, militry_num , firstname, lastname,image from students";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<tr> <td>" . $row["id"] . "</td> <td>"
. $row["militry_num"] .
"</td> <td>". $row["firstname"] .
"</td><td>" . $row["lastname"] . "</td><td>" .
'<img src="data:image/gif;base64,' . $row["image"] . '" />' .
"</td> </tr>";
}
echo "</table>";
}
else{
echo "0 result" . $conn->error;
}
$conn->close();
?>
</table>
</body>
这篇关于从数据库中检索base64图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文