gpt4 book ai didi

php - 从 mysql 检索后图像不显示

转载 作者:行者123 更新时间:2023-11-30 22:03:09 24 4
gpt4 key购买 nike

下面是根据用户名搜索和显示图片的代码:

<?php
$s=0;
if(isset($_POST['submit'])){
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "photost";
$conn=mysqli_connect($servername, $username, $password, $dbname);
$user_id=$_POST['user_id'];
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$query="SELECT `id`,`image`, `file_name` FROM `images` WHERE `u_id`='$user_id'";
$result =mysqli_query($conn, $query);
$s=1;
}
?>
<body>
<div style="width:100%;height:10%;">
</div>
<div align=center>
<form action="" method="POST" enctype="multipart/form-data">
<table style="width:40%">
<tr>
<th align="right">User Id:</th>
<th> <input type='text' name='user_id'></th>
<th align="left"> <input type="submit" name="submit" ></th>
</tr>
</table>
</form>
</div>
<div style="width:80%;height:1000px;margin:auto;">
<?php
if($s==1){
if (mysqli_num_rows($result) > 0) {
$c=1;
while($row = mysqli_fetch_assoc($result)) {
echo "<div style='width:24%;float:left;margin-left:2px;margin-top:5px;'>";
echo "<img style='height:250px;width:80%;margin:auto;' src=getImage.php?id='$row[id]' /><br>";
echo $row["id"] .". ". $row["file_name"]."<br></div>";
}
}
}
?>
</div>


</body>

下面的代码是根据图片id从数据库中获取图片:

<?php
if(isset($_GET['id'])){
$id = $_GET['id'];
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "photost";
$conn=mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
header("content-type: image/jpeg");
$query="SELECT `image` FROM `images` WHERE `id`='$id'";
$result =mysqli_query($conn, $query);
$row = mysql_fetch_assoc($result);

echo $row['image'];
}

?>

尝试了很多解决方案都没有用。我能够在主页中使用标题(“内容类型:图像/jpeg”)显示一张图像。但是在 getimage.php 中使用它时只显示图像图标。如图所示下面:

Screenshot (Images where userid=1)

请给我一些建议......

最佳答案

在您的第一个代码中进行以下更改:

echo "<img style='height:250px;width:80%;margin:auto;' src='data:image/jpeg;base64,".base64_encode( $row['image'] )."'/><br>";

关于php - 从 mysql 检索后图像不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42733607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com