douyangqian5243 2014-06-14 19:09
浏览 114
已采纳

显示从MySQL数据库存储为BLOB数据的所有图像

I have uploaded some images to MySQL using PHP. I can also display 1 image everytime i modify the id in the HTML img tag. But now I'm trying to display all images stored in MySql database and the problem is when i use a 'While Loop' it only shows the text columns and not the images stored as BLOB data in MySQL...

I have a database called: my_db I have a table called: blob And i have 3 columns in my table: id, name & image

here is the code for the index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image"><input type="submit" name="submit" value="Upload">
</form>
<?php

if(isset($_POST['submit']))
{
    mysql_connect("127.0.0.1","root","");
    mysql_select_db("my_db");

    $imageName = mysql_real_escape_string($_FILES["image"]["name"]);
    $imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
    $imageType = mysql_real_escape_string($_FILES["image"]["type"]);

    if(substr($imageType,0,5) == "image")
    {
        mysql_query("INSERT INTO `blob` VALUES('','$imageName','$imageData')");
        echo "Image Uploaded!";
    }
    else
    {
        echo "Only images are allowed!";
    }

}

?>

<img src="showimage.php?id=11">

</body>
</html>

And here is the code for showimage.php:

<?php

mysql_connect("127.0.0.1","root","");
mysql_select_db("my_db");

if(isset($_GET['id']))
{
    $id = mysql_real_escape_string($_GET['id']);
    $query = mysql_query("SELECT * FROM `blob` WHERE `id`='$id'");
    while($row = mysql_fetch_assoc($query))
    {
        $imageData = $row["image"];
    }
    header("content-type: image/jpeg");
    echo $imageData;
}
else
{
    echo "Error!";
}

?>

Thanks in advance :-)

  • 写回答

3条回答 默认 最新

  • dtcu5885 2014-06-14 19:18
    关注

    First you need to remove the if(isset($_GET['id']) statement because you want to display all images.

    Next, query all images by changing your query to query without an id

    $query = mysql_query("select * from `blob`");
    

    Next, store the image data to an array.

    $images = array();
    while ($row = mysql_fetch_assoc($query)) {
      $images[] = $row['image'];
    }
    

    Finally, display all images. (Got the code to display multiple blobs from here)

    /* header should be removed
       header("content-type: image/jpeg"); */
    foreach ($images as $image) {
      echo '<img src="data:image/jpeg;base64,'. base64_encode($image) .'" />';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊