Deepashika
Deepashika

Reputation: 87

How to retrieve image from database and display image on the web page

I have inserted image into database and store name in the table. my image is saved in a folder named 'Uploads'. Now need to retrieve image from the databse and display it. when I try to display It only shows the image name which is taken from my table.but it does not show the image.

retrieving code is given below

$sql="SELECT * FROM candi_profile WHERE can_email='{$_SESSION['usr_email']}'";

$result=mysqli_query($con,$sql);
if(!$result) die(mysqli_error($con));
  <div class="container">

        <!-- Page Header -->
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">Employer Dashboard 
                   
                </h1>
            </div>
        </div>
        <!-- /.row -->

        <!-- Projects Row -->
        
        <div class="row">
            <div class="col-md-4">
        <?php
          while($rows=mysqli_fetch_array($result)){
              $c_id = $rows['can_id'];
            var_dump($c_id);
             ?>
             
            <p class="lead"><?php echo $rows['can_name'] ?></p>
            <div class="profile-sidebar">
                <!-- SIDEBAR USERPIC -->
                
                <div class="profile-userpic">
                        <p class="lead">
                         <?php echo $rows['pic_name'] ?></p>
                </div>
                
                <!-- END SIDEBAR USERPIC -->

                <!-- SIDEBAR USER TITLE -->
                <div class="profile-usertitle">
                    <div class="profile-usertitle-name">
                        Marcus Doe
                    </div>
                    <div class="profile-usertitle-job">
                       <?php echo $rows['can_city'] ?>
                   <i class="glyphicon glyphicon-map-marker">
                        </i>
                    </div>
                        
                    <div class="profile-usertitle-job">
                         <i class="glyphicon glyphicon-envelope"></i>
                      <?php echo $rows['can_email'] ?>
                    </div>
                        
                    <div class="profile-usertitle-job">
                        <?php echo $rows['can_country'] ?>
                    </div>
                </div>
                <!-- END SIDEBAR USER TITLE -->
                
                <!-- SIDEBAR BUTTONS -->
                <div class="profile-userbuttons">
                    <hr>
                </div>
                
                <!-- END SIDEBAR BUTTONS -->
                <!-- SIDEBAR MENU -->
      
            <?php
            }
            ?>          
            </div>

enter image description here enter image description here

Upvotes: 0

Views: 40478

Answers (5)

Sreejith N
Sreejith N

Reputation: 41

  1. First fetch image from database using query
  2. The imagejpeg() function is an inbuilt function in PHP which is used to display image to browser or file.
  3. Get data using function ob_get_contents();
  4. Display image in page with height and width

$id = $_GET['id'];    
$sql = "select image from table where id='".$id."'";    
$res = mysqli_query($sql);    
$row = mysqli_fetch_assoc($res);    

$image = $row['image'];    
ob_start();    
imagejpeg($image, null, 50);        
$data = ob_get_contents();    
ob_end_clean();    
echo "<img src='data:image/jpg;base64,'".base64_encode($data)."' style='border:1px 
black; border-radius:10px; width:100px; height:125px;'>";

Upvotes: 0

Hassan Saeed
Hassan Saeed

Reputation: 7130

friend instead of making images folder you should make a new image column(i.e "imageColumn ") type as blob then You need to create another php script to return the image data, e.g. getImage.php.

home.php(or display image page) code

<body>
<img src="getImage.php?id=1" width="175" height="200" />
</body>

Then getImage.php is

<?php

  $id = $_GET['id'];
  // do some validation here to ensure id is safe

  $link = mysql_connect("localhost", "root", "");
  mysql_select_db("dvddb");
  $sql = "SELECT imageColumn FROM Tablename WHERE id=$id";
  $result = mysql_query("$sql");
  $row = mysql_fetch_assoc($result);
  mysql_close($link);

  header("Content-type: image/jpeg");
  echo $row['imageColumn '];
?>

Upvotes: 0

Vamsi Abbineni
Vamsi Abbineni

Reputation: 479

you can use this code to retrieve image from database

<?php
include 'connection.php'
?>
<?php

$result = mysql_query("SELECT * FROM table") or die(mysql_error()); 


?>

<table border="1" cellpadding="5" cellspacing="5">
<tr> <th>Image</th></tr>

<?php

while($row = mysql_fetch_array($result)) {

$id = $row['id'];

?>
    <tr>

        <td><img src="uploads/<?php echo $row['pic_name'];?>" alt=" " height="75" width="75"></td>

   </tr>

<?php   
} 
}

?>
</table>

Upvotes: 2

Edmar
Edmar

Reputation: 21

I assume that the content of $rows['pic_name'] is string only as said on your question.

Put an image attribute and call the path of the image with the corresponding filename save on the database.

<img src = "<path>/<?php echo $rows['pic_name'] ?>" />

NOTE:

Make sure the image is existing on your desire path.

Upvotes: 2

Owais Aslam
Owais Aslam

Reputation: 1589

Use image tag to display the image and give it path to the image folder

<img src="your path/<?php echo $rows['pic_name'] ?>" />

Upvotes: 1

Related Questions