我必须在我的网站上显示 3 张带有 figcaption 的图像上传到 mysql 数据库(id_user(int),图像(longblob),文本(文本)我为 1 张图片制作了 3 个数字,但我不知道如何显示多于 1 张图片:
$sql = "SELECT image, text FROM images WHERE id_user=?";
$stmt = mysqli_stmt_init($db_con);
if (!mysqli_stmt_prepare($stmt, $sql)){
alerts('SERVER_ERROR');
}
else {
mysqli_stmt_bind_param($stmt,"i", $_SESSION['id']);
mysqli_stmt_execute($stmt);
$result= mysqli_stmt_get_result($stmt);
$result=mysqli_fetch_array($result);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
$text1 = $row['text']; // doesn't work
}
它适用于没有 figcaption(text) 的一张图片,但对于同一个 id_user 我还有 2 张图片,我想像这样显示它:
<figure> image1 </figure> <figcaption> <?php if(!empty($text1)) { echo $text1;} ?></figcaption>
<figure> image2 </figure> fig text2
<figure> image3 </figure> fig text3
如何改变呢? 附言。我知道我不应该将 img 上传到数据库,但它只有 3 张图片。帮我修一下。
最佳答案
使用 mysqli_fetch_all() 获取多行
$result=mysqli_fetch_all($result);
foreach($result as $row){
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>';
}
关于php - 显示来自 MySQL 的多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54774027/