duanbei1709 2016-05-29 09:05
浏览 85
已采纳

从数据库中获取图像并将其显示在屏幕上

I am using phpmyadmin for creating my database. I have stored images in a folder called - "images". The path of the image is stored in the database.

I now want to fetch an image corresponding to an id and display it on the screen.

This is how I am storing my image.

function GetImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype)  { 
 case 'image/bmp': return '.bmp';
 case 'image/gif': return '.gif';
 case 'image/jpeg': return '.jpg';
 case 'image/png': return '.png';
 default: return false;
  }
  }
 if (!empty($_FILES["uploaded_image"]["name"]))
   {
$file_name=$_FILES["uploaded_image"]["name"];
$temp_name=$_FILES["uploaded_image"]["tmp_name"];
$imgtype=$_FILES["uploaded_image"]["type"];
$ext= GetImageExtension($imgtype);
$imagename=date("d-m-Y")."-".time().$ext;
$target_path = "images/".$imagename;
if(move_uploaded_file($temp_name, $target_path)) {
$query="insert into users(images_path,submission_date,image_name)values('".$target_path."','".date("Y-m-d")."','$imagename')";

Now, I want to fetch the image and display it on the screen.This is the code I have written -

 $connection = mysql_connect("localhost", "root", "");
  $db = mysql_select_db("project", $connection);
  $query = mysql_query("select * from users where _id= '$r'");
    $rows = mysql_num_rows($query);
  if ($rows == 1 ) {
  $row1=mysql_fetch_assoc($query);
  $image=$row1["images_path"];
  }

What should I write after this so that the image is displayed?

  • 写回答

4条回答 默认 最新

  • dqfsbvd43312 2016-05-29 09:09
    关注

    Use the image in an image tag like you normally would with an image URL.

    echo '<img src="/'.$image.'" alt="an image"/>';
    

    You may need to adjust the image path to be relative to the site root by prepending /path/to/images/ if your "images" folder is not in the site root.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

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