0

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

4
  • 1
    Add your full path where you store image.Like http://localhost/test/images/test1.php Commented Jul 25, 2016 at 6:10
  • you have to store only paht of the images in database not an image. Commented Jul 25, 2016 at 6:11
  • this sort of question has been asked thousands of times before Commented Jul 25, 2016 at 6:12
  • 1
    Possible duplicate of How to retrieve images from MySQL database and display in an html tag Commented Jul 25, 2016 at 6:23

5 Answers 5

2

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> 
Sign up to request clarification or add additional context in comments.

2 Comments

don't you think comments given above states same thing? BTY unnecessary code
using table for formating display like this is very bad code, should use CSS and use div tags and format correctly.
2

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.

Comments

1

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

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

3 Comments

I think in your comment it is returning the path of image not displaying it.
( <?php echo '/images/'.$rows['pic_name'] ?> ) this is your comment.
i don't think it will display the image.
0

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 ']; ?> 

Comments

0
  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;'>"; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.