TeeKay
TeeKay

Reputation: 1065

Fetch an image from database and display it on screen

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?

Upvotes: 0

Views: 762

Answers (4)

Manish Dhruw
Manish Dhruw

Reputation: 803

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

OR

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

Upvotes: 0

Poiz
Poiz

Reputation: 7617

Simply build up an Image using the $image as the SRC attribute and then echo it back like so:

    <?php
        $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);

        $imgHTML    = "";   // INITIALIZE THE IMAGE HTML TO NOTHING SO THAT YOU CAN STILL ECHO THIS VARIABLE IF THERE IS NO IMAGE

        if ($rows == 1 ) {
            $row1       = mysql_fetch_assoc($query);
            $image      = $row1["images_path"];

            // JUST BUILD AN HTML REPRESENTATION OF YOUR IMAGE LIKE YOU WOULD IN NORMAL HTML BUT WITH PHP
            $imgHTML    = "<img alt='ALTERNATIVE_IMAGE_NAME' class='img_class' id='img_id' src='" . $image . "' />";
        }

        // NOW SIMPLY DISPLAY THE IMAGE.....
        echo $imgHTML;

Upvotes: 0

m33ts4k0z
m33ts4k0z

Reputation: 703

If you get the correct path after your query, you can use the HTML tag with the parameter src= and the variable holding the path to your image. You need to know the name of the image of course

Upvotes: 0

Chris
Chris

Reputation: 5876

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.

Upvotes: 1

Related Questions