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条)

报告相同问题?

悬赏问题

  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义